Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you select all id tags that contain a certain string in javascript?

I have an ASP.NET application that renders multiple questions with the option to provide an explanation for the answers given.

<label for="[<%:count %>].AnswerExplanation_<%: i+1 %>" id="[<%:count %>].toggleExplanation_<%: i+1 %>"><strong>Add Explanation</strong></label>
<br /><br />
<div id="[<%:count %>].Explanation_<%: i+1 %>">
    <textarea id="[<%:count %>].AnswerExplanation_<%: i+1 %>" name="[<%:count %>].AnswerExplanation_<%: i+1 %>" class="ckedit"></textarea>
</div>

so you will have id's like "[X].toggleExplanation_Y" corresponding to "[X].AnswerExplanation_Y"

I am writing a javascript function to show/hide the AnswerExplanation divs, and was looking for a way to select every id containing "toggleExplanation" I should be able to get the rest from there.

like image 925
Alex Avatar asked Nov 14 '11 18:11

Alex


People also ask

How do I get all elements of a specific ID?

Use the document. querySelectorAll() method to get all elements whose id starts with a specific string, e.g. document. querySelectorAll('[id^="box"]') . The method returns a NodeList containing all the elements that match the provided selector.

How do you select all elements by tag?

The * selector selects all elements. The * selector can also select all elements inside another element (See "More Examples").

How do you select an element with ID data '?

Use the querySelector method to get an element by data attribute, e.g. document. querySelector('[data-id="box1"]') . The querySelector method returns the first element that matches the provided selector or null if no element matches the selector in the document.

What is ID selector in Javascript?

Definition and Usage The #id selector selects the element with the specific id. The id refers to the id attribute of an HTML element. Note: The id attribute must be unique within a document. Note: Do not start an id attribute with a number.


1 Answers

Try using the attribute contains collector

$('label[id*="toggleExplanation"]')
like image 69
John Hartsock Avatar answered Oct 04 '22 19:10

John Hartsock