I am working with an array of selectors:
var selectors = ['#first_name', '#last_name'];
I want to be able to bind events such as focus
, change
, keyup
, etc to each selector in that array. The selectors are attached to form inputs.
For binding each selector I have the following code.
for(var selector in selectors){
$('#form').find(selectors[selector]).on('change keyup click select focus', function(){
console.log(selectors[selector]); //this is here for testing
});
}
This should logically work but when I read the console log it shows #last_name
twice.
I have checked the form over and over, and it has nothing to do with the form. The only problem in my code is this single logic structure that I have provided. If I console log the array before the loop it shows the array as is ['#first_name', '#last_name']
, so that is why I am so confused.