Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery find all ids starting with a string?

Just wondered how I would search for all the ids starting with "content_" in the whole page and also a way to only find them in a named div called "extra_content". Once i have all the ids i want to hide them.

Below is an example of what i want to find.

<div id="content_1"></div> <-- Find <div id="content_2"></div> <-- Find <div id="contet_3"></div>  <div id="extra_content">      <div id="content_extra_1"></div> <-- Find     <div id="content_extra_2"></div> < -- Find </div> 

Examples would be helpful.

Thanks

like image 722
baggins Avatar asked Apr 11 '11 04:04

baggins


People also ask

How to get all elements with same id in jQuery?

How to get all elements with same ID in jQuery? Answer: Use the jQuery attr() Method You can simply use the jQuery attr() method to get or set the ID attribute value of an element.

How do you select all elements with an ID?

The id selector uses the id attribute of an HTML element to select a specific element. The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.

How can I get ID in jQuery?

The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.


1 Answers

Use the attribute-starts-with selector:

$('[id^="content_"]').hide(); 

To limit the search to elements within extra_content:

$('#extra_content [id^="content_"]').hide(); 
like image 129
David Tang Avatar answered Sep 20 '22 08:09

David Tang