fala
fala

Reputation: 271

Add id to a child of a visible element

How can I write in jQuery this:

If .horse is visible, then add #cat to .dog (but only to .dog which is child of the visible .horse)?

<div id="tabs-1" class="horse" style=" margin-right: 20px; display: none;">
    <div style = "width:70%; margin: 0 auto; margin-top:-20px">
       <div class="rabbit">
           <a class="dog" href="movie.mov"></a>
       </div>
    </div>
</div>

<div id="tabs-2" class="horse" style=" margin-right: 20px; display: block;">
    <div style = "width:70%; margin: 0 auto; margin-top:-20px">
       <div class="rabbit">
           <a class="dog" href="movie.mov"></a>
       </div>
    </div>
</div>

Upvotes: 1

Views: 116

Answers (4)

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67217

Try,

$('.horse:visible .dog').append($('#cat'));

The above code would append #cat into .dog which is a descendant of visible .horse

If you want to add id to the particular element then do,

$('.horse:visible .dog').attr('id','cat');

Upvotes: 1

Josh Harrison
Josh Harrison

Reputation: 6004

If you want to add the ID cat to .dog, use this:

$(".horse:visible .dog").attr("id", "cat");

Here is an example.

Upvotes: 1

Mritunjay
Mritunjay

Reputation: 25892

Use following, it will work

$('.horse:visible .dog').attr('id','cat')

Upvotes: 4

James Donnelly
James Donnelly

Reputation: 128786

We can combine jQuery's :visible selector with jQuery's attr() method to set the id:

$('.horse:visible .dog').attr('id', 'cat');

This will give the .dog element contained within your visible .horse element an id of "cat".

Upvotes: 0

Related Questions