Mohamed Samy
Mohamed Samy

Reputation: 941

Target Parent Div with child Div class with Jquery

i have this code : what i need is target the first div.General with using the child div.myyyy-classs , of course without using any ID

<div class="General">
<div class="">hello</div>
<div class="">hello</div>
<div class="">hello</div>
</div>

<div class="myyyy-classs">hello</div>


<div class="General">
<div class="none">hello</div>
<div class="none">hello</div>
<div class="none">hello</div>
<div class="none">hello</div>
</div>

what i done is

$("#Template1").click(function(){
     $(".myyyy-classs").closest(".General").html("Its woking")  
});

Upvotes: 0

Views: 491

Answers (2)

Hamza Dhamiya
Hamza Dhamiya

Reputation: 1287

Update:

After your update you will use .sibling() for that.

$("#Template1").click(function(){
     $(".myyyy-classs").siblings(".General").html("Its woking")  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id='Template1'>Click Me</span>
<div class="General">
<div class="">hello</div>
<div class="">hello</div>
<div class="">hello</div>
</div>
<div class="myyyy-classs">hello</div>

<div class="General">
<div class="none">hello</div>
<div class="none">hello</div>
<div class="none">hello</div>
<div class="none">hello</div>
</div>

Update 2 The above one will select both .General.Secondly, if you want to select previous .General use .prev('.General') or/and for next you can use .next('.General').

Further .prevAll() and .nextAll() are the functions to select all the previous elements of match element or next elements relatively.

Upvotes: 1

Tomer
Tomer

Reputation: 17930

Seems like the .General is not a parent div but rather a sibling, so use this:

 $(".myyyy-classs").siblings(".General");

Please note that this will return all the siblings as an array, so if you want only the first one use this:

$(".myyyy-classs").siblings(".General").get(0);

Upvotes: 0

Related Questions