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?
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.
ID and Element selector are the fastest selectors in jQuery.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With