I’m new to jQuery and am still learning the syntax. I need help looping over all of the checkboxes in a specific form and remove those elements that I set with a “value” of x from the server. The syntax I have so far, which is not working is:
$('#FormA > :checkbox').each(function() {
var i = $(this);
if(i.attr('value') == "-99")
{
i.remove();
}
});
It looks after reading the API that under the hood the .each() method does a “java enhanced for loop” style iteration on all of the DOM elements that I pass into jQuery. Not sure though…
Thanks for your help!
Most jQuery methods are applied to every element in the matched set. To do what you are trying to do, you just need to select the right set of elements and call
remove. There’s no need for theeach. For example:This uses an “attribute equals” selector to find checkbox input elements with a
valueof-99. It then removes all of the selected elements from the DOM.The docs for
removemake this clear: