I’ve got some elements like
<div id="myDiv">
<p><input type="checkbox" />text</p>
<p>special text</p>
<p><input type="checkbox" />text</p>
<p><input type="checkbox" />text</p>
</div>
My aim is to check the input-boxes below the ‘special text’.
Therefore I created a variable findText to find ‘special text’
var findText = $("#myDiv:contains('special text')");
I’m checking whether ‘special text’ exists and if so, I want the next input elements to be checked. .nextAll() unfortunately just gets the following siblings. But how can I call the next elements?
var findText = $("#myDiv:contains('special text')");
if(findText.length > 0) {
findText.nextAll("input").attr("checked",true)
}
Thank you for your time and help.
nextAllwith afindshould do it:That finds the following paragraphs, and their descendant inputs. (Or use
childrenrather thanfindif the inputs are guaranteed to be immediate children of the paragraphs.)…but I think your selector for
findTextis incorrect, try:Putting that all together:
Live copy
Separately, you might consider limiting the search to just checkboxes with jQuery’s custom
:checkboxselector:Live copy