Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery dynamic selector

Tags:

I have some code which uses a selector in a loop.

This works:

document.getElementById("new_grouping_"+i).value 

This does not: $("#new_grouping_"+i).value

Is there a way to do this using jQuery?

like image 691
Devin Ceartas Avatar asked Aug 05 '09 01:08

Devin Ceartas


People also ask

Does jQuery work on dynamic content?

You have to add the selector parameter, otherwise the event is directly bound instead of delegated, which only works if the element already exists (so it doesn't work for dynamically loaded content). The jQuery set receives the event then delegates it to elements matching the selector given as argument.

Which jQuery selector is fastest?

ID and Element selector are the fastest selectors in jQuery.

Can jQuery selector be a variable?

Projects In JavaScript & JQueryYes, it is possible to pass a variable into a jQuery attribute-contains selector. The [attribute*=value] selector is used to select each element with a specific attribute and a value containing a string.


1 Answers

You should use the val() function:

var myValue = $("#new_grouping_"+i).val(); // to get the value  $("#new_grouping_"+i).val("something");    // to set the value  
like image 180
Christian C. Salvadó Avatar answered Oct 18 '22 01:10

Christian C. Salvadó