Count Dynamically created html elements with jquery
Asked Answered
E

3

9

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 accomplish.

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

Et answered 19/12, 2012 at 3:25 Comment(8)
If you have the element id you don't need to add an html tag before. So $('#participant-' + i) is better.Promoter
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..Roseannroseanna
Where is your event binding code?Redundant
Can you show the code that your for is placed ?Promoter
How do you get the 25 ? Change it by $("input[id^='participant-']").lengthGeoid
Thanks Alot everyone! After putting it in the jsfiddle. I found that when I added the new buttons I missed a piece of code. The new elements didn't follow the same naming convention, so when I tried to search for it using the selector it didn't find it. A small over look...Et
Can you show us the code that dynamically creates the element? Do you have a chance to hook on that or do you need to use mutation events?Sartorial
Please see updated Question i have added jsfiddle with follow up questionEt
R
8

Give it a common 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);
Redundant answered 19/12, 2012 at 3:27 Comment(5)
Would be better than searching the dom for $('#participant-' + i).val() many timesRedundant
OP mentioned about counting Dynamically created elements, but he never show the part he is binding the event. So could't help on that part.Redundant
accepting because taught me something about efficiency and made it more readable.Et
Please see updated Question i have added jsfiddle with follow up questionEt
I had to use console.log(values.length) to get an actually number, but the solution totally works for my application!Patrizio
A
3

Could also consider the use of the CSS attribute selector.

http://www.w3.org/TR/CSS2/selector.html#attribute-selectors

    $("input[id|=participant]").each(function(){
            // something
    });
Aceldama answered 19/12, 2012 at 4:20 Comment(1)
Please see updated Question i have added jsfiddle with follow up questionEt
K
-3

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

Kulturkampf answered 25/11, 2014 at 20:7 Comment(1)
it's not dynamic and does not change if we add other input via jsBuster

© 2022 - 2024 — McMap. All rights reserved.