3

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

1
  • 2
    DOM is at tree. Every node knows what its parent is, and what children it has. The click event will speciflcally tell you WHICH of those buttons was clicked, and then it's a simple tree operation to move to the sibling img. Commented Mar 15, 2016 at 19:01

5 Answers 5

4

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()
});
Sign up to request clarification or add additional context in comments.

Comments

1

Try to use .next() at this context,

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

Since the image that we need to target is the next immediate sibling of the clicked button.

Comments

1
$("button").click(function() {
    $(this).siblings('img').hide(); 
});

This is only dependent on the current node's structure, and it doesn't matter if the image is before or after the button. You can read more about siblings() here, and a live code example here

Comments

1
I think you are looking for this..
$("button").click(function() {
    $(this).next('img').hide();
});

Comments

1

I think this is what you want, see fiddle: https://jsfiddle.net/DIRTY_SMITH/7oe5kh9L/26/

HTML

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

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

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

JQuery

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

4 Comments

I'm honestly curious, what's the purpose of posting my exact answer, which already has a live link, just 3 minutes later. I'm all for everyone posting different solutions to the same problem so that OP can choose what's best suited and future readers can have options, but what value does this add?
Maybe he worked on the answer for 10 minutes, 3 too late to be the first, and then decided not to trash it or maybe just didn't see yours. Nothing wrong in that. Let the upvotes decide :) IMHO none of these answers should have been written in the first place.
@SergiuParaschiv that is exactly what happened, its like you are a mind reader or something! lol
@Pabs123 when creating a fiddle on another page and coming back to SO, unless you refresh the page the "new" answer's will not apear, I am sorry you got all bent outta shape, was not my intention to post something you already did, however, it really doesn't matter. Cheer up buddy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.