Reputation: 4829
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?
Upvotes: 23
Views: 70824
Reputation: 401
# is important for selector. Here I am setting value to span.
var i="spanId";
$("#"+ i).html("hello").show();
Upvotes: 2
Reputation: 827218
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
Upvotes: 28
Reputation: 61557
$("#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
Upvotes: 12