Eitan
Eitan

Reputation: 1386

changing several objects with jquery

I am looking for a solution for following:

For JQuery: How can I change the attribute of all parents of objects that have the same name?

Here is my code:

<div class="roundedOne">
  <input type="radio" id="id_single" name = "radStatus" value = "1" />
  <label for="id_single"></label>
</div>          

<div class="roundedOne">
  <input type="radio" id="id_married" name = "radStatus" value = "1" />
  <label for="id_single"></label>
</div>  

I want to do something like : $("[name='radStatus']").parent().attr("class", "myNewClassName");

Thanks :)

Upvotes: 1

Views: 58

Answers (3)

Brett McCarty
Brett McCarty

Reputation: 399

You can use the .parents() jQuery method.

So this:

$("[name='radStatus']").parent().attr("class", "myNewClassName");

Becomes this:

$("[name='radStatus']").parents().attr("class", "myNewClassName");

Here is a working Fiddle that will change all the parents of the elements. Inspect the elements to see the new class name.

Upvotes: 1

Suresh Atta
Suresh Atta

Reputation: 121998

Try

$("input[name ='radStatus']").parent().addClass("myNewClassName");

Upvotes: 1

Amit
Amit

Reputation: 15387

Try this

 $(function(){
    $("[name='radStatus']").parent().attr("class", "myNewClassName");
 });

Demo

Your code is working. but You need, how to implement. see demo

Upvotes: 2

Related Questions