Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to target a div by class name inside another class

I have some divs like this.

<div class"parent">
    <div class"child">
     Some Stuff Here
    </div>
</div>


<div class"parent">
    <div class"child">
     Some other kinda Stuff Here
    </div>
</div>

I want to click parent class and show the child class only inside that parent without showing the other children classes in other parent classes.

$(document).on('click', '.parent', function(){
    $(this).find($('.child').show(500));
});
like image 354
user3109875 Avatar asked Dec 25 '22 07:12

user3109875


2 Answers

Pass a selector string to find() not an object - you are passing a jQuery object. You also have invalid HTML because class"parent" should be class="parent".

Demo

$(document).on('click', '.parent', function(){
    $(this).find('.child').show(500);
});
like image 95
MrCode Avatar answered Dec 29 '22 09:12

MrCode


First of all you need to correct your markup as there should be = between attribute class and its value. So markup should be like :

<div class="parent">
    <div class="child" >
     Some Stuff Here
    </div>
</div>

Try this :

$(document).on('click', '.parent', function(){
    $(this).children('.child').show(500);
});

Demo

like image 35
Bhushan Kawadkar Avatar answered Dec 29 '22 10:12

Bhushan Kawadkar