Skip to main content

Query Elements by a Part of id or name Attribute in jQuery

When using jQuery, you often need to query elements by their id or name attribute. This can be done with the help of the attribute equals selector.

For example, if you want to find all elements with an id that ends with “wrapper”, you can use the following selector:

$("div[id$='wrapper']")

If you want to find all elements with an id that contains “product”, you can use the following selector:

$("div[id*='product']")

You can also use the “attribute starts with” selector to find elements that have an id that starts with a certain string. For example, if you want to find all elements with an id that starts with “item_”, you can use the following selector:

$("div[id^='item_']")

You can loop through found elements like this:

$('#table-data tr[id*="product"]').each(function(){
  console.log($(this).text());
});

$('input[name*="product_field"]').each(function(){
  console.log($(this).val());
});

By continuing to use the site, you agree to the use of cookies.