Reputation: 385
Suppose I want to increase jQuery any attribute number by 1 lets say.
For example
<label for='question_1' class='question_content_label'>
<input type='text' id='question_1' class='question_content'>
I want to increase its id by 1 so it becomes
<label for='question_2'>
<input type='text' id='question_2'>
I thought i would do in jQuery like this:
$('body').find('.question_content').attr('for',function (index,name){
return name.replace(/(\d+)/, function(fullMatch,n){
return Number(n) + 1;
console.log(n);
});
});
$('body').find('.question_content_label').attr('for',function (index,name){
return name.replace(/(\d+)/, function(fullMatch,n){
return Number(n) + 1;
console.log(n);
});
});
I don't want to repeat this:
function (index,name){
return name.replace(/(\d+)/, function(fullMatch,n){
return Number(n) + 1;
console.log(n);
});
};
I thought creating function like this
function increment_attr_number(index,name){
return name.replace(/(\d+)/, function(fullMatch,n){
return Number(n) + 1;
console.log(n);
});
};
And passing like this:
$('body').find('.question_content_label').attr('for','increment_attr_number');
but it don't work
Thanks:)
Upvotes: 1
Views: 29
Reputation: 171679
You need to pass the function reference, not a string representation of it
$('body').find('.question_content_label')
.attr('for', increment_attr_number);
Upvotes: 4