JavaScript javascript - addEventListener with an array of input javascript addeventlistener with parameters,javascript addeventlistener with this,javascript addeventlistener

I need to use addEventListener for automatically updating the values typed into different input text (actually 4 input tags)

Here's the HTML code :

<td class="col-md-4">
        <label for="lab1"><b>$\mathbf{M_{1}}$</b></label>                                
        <input type="text" class="form-control input-sm" id="lab1" />                    
      </td>
        <td class="col-md-4">                                                            
        <label for="lab2"><b>$\mathbf{M_{2}}$</b></label>
        <input type="text" class="form-control input-sm" id="lab2"  />                   
      </td>
    <td class="col-md-4">                                                                
        <label for="lab3"><b>$\mathbf{M_{3}}$</b></label>                                
        <input type="text" class="form-control input-sm" id="lab3" />                    
      </td>
      </tr>
   <tr>                                                                                  
       <td class="col-md-4">                                                             
        <label for="lab4"><b>$\mathbf{\Theta_{1}}$</b></label>                           
        <input type="text" class="form-control input-sm" id="lab4" />                    
      </td>

I would like to update values when they are modified, without having to click outside the input.

I tried to use the following addModifyEvent function in main :

    function addModifyEvent() {                                                              

     var inputList = document.querySelectorAll('input.form-control');   

    for (var i=0; i<inputList.length; i++)                                                   
     {  
    inputList[i].addEventListener('input', function()                                        
        {   
console.log(inputList[i].value);  });
    }       
    }     

but it doesn't work ( I get TypeError: inputList[i] is undefined).

I don't know if I shoud better do :

inputList[i].addEventListener('inputList['+i+']', 

or something closed instead of

inputList[i].addEventListener('input', 

I want to add an eventlistener for each input tag.

Anyone could tell me the right syntax ?

Thanks

Answer:1

As you can see in example i printed 2 console one for your input and one for your i

So your problem is:

when in listener function you are trying to access console.log(inputList[i].value); so at that time your i is 4 and your input array length is 0-3 and because of for loop i will be incremented to 4 so you got undefined because inputList[4] is not present in your array.

Working example:http://jsfiddle.net/kevalbhatt18/gqce9htx/

var input = document.querySelectorAll('input');
  console.log(input)
   for(var i=0;i<input.length;i++){
      input[i].addEventListener('input', function()
      {  
         console.log(input)
         console.log(i)
         console.log('input changed to: ', this.value);
      });

 }

debugg your error example:http://jsfiddle.net/kevalbhatt18/gqce9htx/2/

Answer:2

The context of the event handler is the input element the event is fired on. Replace the inputList[i] with this:

console.log(this.value);
Answer:3

A slightly modified version of your code that uses this.value works just fine:

function addModifyEvent() {
    var inputList = document.querySelectorAll('input.form-control');
    for (var i = 0; i < inputList.length; i++) {
        inputList[i].addEventListener('input', function () {
            console.log(this.value);
        });
    }
}

Demo: http://jsfiddle.net/jfriend00/9ny3wcxu/


You can't refer to inputList[i] inside the event handler because by the time the event handler is called (sometime later) the for loop has already finished running so i points past the end of the inputList HTMLCollection object.

You can solve the problem by just using this as a pointer to the object that caused the event and thus this.value is the value of the input field that has just been modified.

Answer:4

At the time the event fires,

console.log(inputList[i].value);

inputList[i] will not be valid as it was part of the for loop used to bind event listeners.

Use this to get the current element on which the event was fired,

console.log(this.value);

Full code

function addModifyEvent() {
    var inputList = document.querySelectorAll('input.form-control');
    for (var i = 0; i < inputList.length; i++) {
        inputList[i].addEventListener('input', function () {
            console.log(this.value);
        });
    }
}
Answer:5

You can use closures (if not familiar, please search it and read a bit). The code goes as below:

for (var i=0; i<inputList.length;i++)                                                   
{
     (function(i){
            inputList[i].addEventListener('input',function(){   
                  console.log(inputList[i].value);  });
         }
    })(i);
}  

Basically the value of i remains intact within the function. This is required whenever you do something in a loop that is Async.
The benefit being that you do not need to change the way you access values, except that you need to enclose them within a closure function.
Hope this solves you issue without any changes in the way to want to access the array.

Answer:6

I've got an event listener in my Collection which listens for the 'add' event, which only seems to fire when the sample data I add in my router is added, but not when I add more models in my view, the ...

I've got an event listener in my Collection which listens for the 'add' event, which only seems to fire when the sample data I add in my router is added, but not when I add more models in my view, the ...

  1. backbone collection fetch
  2. backbone collection reset
  3. backbone collection add
  4. backbone collection filter
  5. backbone collection sort
  6. backbone collection events
  7. backbone collection each
  8. backbone collection set
  9. backbone collection get
  10. backbone collection comparator
  11. backbone collection length
  12. backbone collection to array
  13. backbone collection find by id
  14. backbone collection save
  15. backbone collection extend
  16. backbone collection foreach
  17. backbone collection parse
  18. backbone collection remove
  19. backbone collection where
  20. backbone collection first

I need to open a .html file upon password being entered correctly, the file is in the same folder. I have gotten most of it to work but it now says that my password function is not a function. Any ...

I need to open a .html file upon password being entered correctly, the file is in the same folder. I have gotten most of it to work but it now says that my password function is not a function. Any ...

  1. javascript password check
  2. javascript password check strength
  3. javascript password check regex
  4. javascript check password match
  5. javascript check password complexity
  6. javascript check password length
  7. javascript check password for special characters
  8. javascript check password equal
  9. javascript check password letters numbers
  10. javascript check password input
  11. javascript check password validation
  12. javascript check password while typing
  13. javascript check password characters
  14. javascript check password format
  15. javascript check password fields match
  16. javascript check password pattern
  17. javascript check password field
  18. javascript check password values
  19. javascript password confirmation check
  20. javascript password policy check

I have a web design question that I'm trying to puzzle out. Essentially I'm looking to design a navbar such that it will change what buttons/links are displayed in the navbar based on what type of ...

I have a web design question that I'm trying to puzzle out. Essentially I'm looking to design a navbar such that it will change what buttons/links are displayed in the navbar based on what type of ...

I have a common header for all of the pages I have in my site. When I click on any link it takes me to that page and the page gets refreshed. But the header is the same as it was in the previous ...

I have a common header for all of the pages I have in my site. When I click on any link it takes me to that page and the page gets refreshed. But the header is the same as it was in the previous ...

  1. change active link color css
  2. change active link color
  3. change active link when scrolling
  4. change active link color css bootstrap
  5. change active link color bootstrap
  6. change active link in navbar
  7. change active link color javascript
  8. change active link color divi
  9. change active link jquery
  10. change active link
  11. change active link html
  12. change link color active page css
  13. html change active link color
  14. change color active link using jquery
  15. bootstrap change active link
  16. change link color on active page
  17. css change active link
  18. bootstrap navbar change active link
  19. bootstrap navbar change active link color
  20. change background color active link css