user205892
user205892

Reputation: 311

JQuery: Select elements that are before an anchor with a specific text

I have a div having a css class "MyClass". Inside this div, I can have any number of checkboxes immediately followed by an anchor having some specific text (lets say MyText for the example).

How can I select all the checkbox elements using JQuery. What I have is

$('div.givenclass input:checkbox')."I DON"T KNOW WHAT TO PUT HERE"

I want to make sure the the checkboxes I get are immediately before an anchor tag with text "MyText".

Please help!

Upvotes: 1

Views: 2179

Answers (2)

Anurag Uniyal
Anurag Uniyal

Reputation: 88865

This will check all checkbox matching it

$('div.givenclass input:checkbox').attr('checked', true);

To restrict before 'my text' achor you can do .prev or check for elements in .each, but better would be to group related checkbox in a div, and select all checkbox in that div

Upvotes: 0

cletus
cletus

Reputation: 625475

$("div.MyClass a:contains(specific test)").prev(":checkbox");

Upvotes: 5

Related Questions