I am counting the number of inputs on the current document that have value. It works fine, except for when I have dynamically added more inputs. I can't get there values.
For example I may have
<input id="participant-1"/>
<input id="participant-2"/>
...
Dynamically created after button click
<input id="participant-15" />
I'll get the value of each one in a for loop like
for(var i =1 ; i <25; i++)
{
...$('input#participant-' + i).val();
}
Now when I run a for loop to check the value of each one of these inputs it only gets the values of the inputs that weren't dynamically created. I have looked at the other questions on here and I still can't see how to apply something like .on()
to what I am trying to acplish.
NEW FOLLOW UP QUESTION ok, now I think this is where I need more clarification concerning how to use the .on.
I have a jsfiddle here: JsFiddle example
where I create new elements and on blur of all text boxes I would like to calculate how many of the elements have value and log it. Now it currently will respond from blur event with elements who were static. It doesn't work for dynamically created elements
I am counting the number of inputs on the current document that have value. It works fine, except for when I have dynamically added more inputs. I can't get there values.
For example I may have
<input id="participant-1"/>
<input id="participant-2"/>
...
Dynamically created after button click
<input id="participant-15" />
I'll get the value of each one in a for loop like
for(var i =1 ; i <25; i++)
{
...$('input#participant-' + i).val();
}
Now when I run a for loop to check the value of each one of these inputs it only gets the values of the inputs that weren't dynamically created. I have looked at the other questions on here and I still can't see how to apply something like .on()
to what I am trying to acplish.
NEW FOLLOW UP QUESTION ok, now I think this is where I need more clarification concerning how to use the .on.
I have a jsfiddle here: JsFiddle example
where I create new elements and on blur of all text boxes I would like to calculate how many of the elements have value and log it. Now it currently will respond from blur event with elements who were static. It doesn't work for dynamically created elements
Share Improve this question edited Dec 19, 2012 at 5:03 Abdullah Rasheed asked Dec 19, 2012 at 3:25 Abdullah RasheedAbdullah Rasheed 3,7524 gold badges34 silver badges51 bronze badges 8-
If you have the element id you don't need to add an html tag before. So
$('#participant-' + i)
is better. – Ricardo Lohmann Commented Dec 19, 2012 at 3:27 - 2 The examples you have and the code should work just fine.. please add more of the code. How you create the duplicates, when you request the values etc. a jsfiddle would be great.. – Gabriele Petrioli Commented Dec 19, 2012 at 3:27
- Where is your event binding code? – Akhil Sekharan Commented Dec 19, 2012 at 3:28
-
Can you show the code that your
for
is placed ? – Ricardo Lohmann Commented Dec 19, 2012 at 3:29 - How do you get the 25 ? Change it by $("input[id^='participant-']").length – Bali Balo Commented Dec 19, 2012 at 3:31
3 Answers
Reset to default 8Give it a mon class:
<input class="textbox" id="participant-1"/>
<input class="textbox" id="participant-2"/>
And get it like:
var values = [];
$('.textbox').each(function(){
values.push($(this).val());
});
console.log(values)
And to answer the edit:
The Syntax should be : $(container_selector).on(event_type, target_selector, callback)
JSFiddle Demo
$('.name').on('blur', 'input', calculate_total);
Could also consider the use of the CSS attribute selector.
http://www.w3/TR/CSS2/selector.html#attribute-selectors
$("input[id|=participant]").each(function(){
// something
});
Using a class selector will save time here.
<input id="participant-1" class="participant"/>
<input id="participant-2" class="participant"/>
Then use a simple count call...
var count = $('.participant').length
alert ('You have ' + count + ' Counted Inputs');
//result is 2
Hope you find this useful