tags:

views:

668

answers:

2

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?

+2  A: 

$("#new_grouping_"+i).val() gets you the value of a form.
$("#new_grouping_"+i).text() gets you the text of an html element.
$("#new_grouping_"+i).html() gets you the html of an html element.

$("#new_grouping_"+i).val('value') sets the value of a form.
$("#new_grouping_"+i).text('value') sets the text of an html element.
$("#new_grouping_"+i).html('value') sets the html of an html element.

$("#new_grouping_"+i).append('value') prepends something at the beginning of an element $("#new_grouping_"+i).append('value') appends something at end of an element

$("#new_grouping_"+i).before('value') places something before an element $("#new_grouping_"+i).after('value') places something after an element.

See More: jQuery Manipulation

Chacha102
Firefox gives me: "$("#new_grouping_desc_"+i).value is not a function" when I add the empty paren after value
Devin Ceartas
Updated*. Was two letter off of the actual function.
Chacha102
+6  A: 

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
CMS