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

8
  • If you have the element id you don't need to add an html tag before. So $('#participant-' + i) is better. 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.. Commented Dec 19, 2012 at 3:27
  • Where is your event binding code? Commented Dec 19, 2012 at 3:28
  • Can you show the code that your for is placed ? Commented Dec 19, 2012 at 3:29
  • How do you get the 25 ? Change it by $("input[id^='participant-']").length Commented Dec 19, 2012 at 3:31

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

5 Comments

Would be better than searching the dom for $('#participant-' + i).val() many times
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.
accepting because taught me something about efficiency and made it more readable.
Please see updated Question i have added jsfiddle with follow up question
I had to use console.log(values.length) to get an actually number, but the solution totally works for my application!
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
    });

1 Comment

Please see updated Question i have added jsfiddle with follow up question
-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

1 Comment

it's not dynamic and does not change if we add other input via js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.