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.
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.
The * selector selects all elements. The * selector can also select all elements inside another element (See "More Examples").
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.
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.
Try using the attribute contains collector
$('label[id*="toggleExplanation"]')
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