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
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.
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.
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.
Use the attribute-starts-with selector:
$('[id^="content_"]').hide();
To limit the search to elements within extra_content
:
$('#extra_content [id^="content_"]').hide();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With