Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How execute script in specific div

So, in dom like this

<div>
    <button>Click me</button>
    <img src=#>
</div>

<div>
    <button>Click me</button>
    <img src=#>
</div>

<div>
    <button>Click me</button>
    <img src=#>
</div>

And script

$("button").click(function() {img.hide() });

How to make js to be executed only in div which contains clicked button? dom is generated, so we cant use specific classes or id's

like image 908
Janusz Bieraszewski Avatar asked Dec 02 '25 13:12

Janusz Bieraszewski


1 Answers

You can use parent() and find() methods.

$("button").click(function () {
    $(this).parent().find('img').hide()
});

Or next() method like following.

$("button").click(function () {
    $(this).next('img').hide()
});
like image 183
Ibrahim Khan Avatar answered Dec 05 '25 03:12

Ibrahim Khan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!