user4575494
user4575494

Reputation:

jquery propertychange on field where ID exist in Array

Hello and thanks for reading this post.

I have a little jquery code that contains an array with the "ID's" of the some fields from the html page

var FieldArray = [
        'txtname',
        'txtpass'
    ];

Normally I would check the fields property change this way:

$('#txtname').bind('input propertychange', function () {
    //code
});

But is there some way, that I can rewrite the code so it check if property changed only on those fields where the id exist in the array.

This dont work but just as an idea about what I want:

FieldArray[value].bind('input propertychange', function () {
    //code
});

Upvotes: 1

Views: 53

Answers (2)

raviture
raviture

Reputation: 1059

You can use followed :

$('#' + FieldArray.join(', #')).bind('input propertychange', function () {
    //code
});

Here we are using multiple selectors by joining the ids from FieldArray with , and '#'

Upvotes: 0

Denys Séguret
Denys Séguret

Reputation: 382334

Yes, you can do this for example :

FieldArray.forEach(function(id){
    $('#'+id).bind('input propertychange', function () {
       //code
    });
});

If the function to call back isn't specific to your id, you can also do

$(FieldArray.map(document.getElementById.bind(document)))
.bind('input propertychange', function () {
   //code
});

Upvotes: 4

Related Questions