4

I am displaying a container <div class="expand"> that - when clicked on - expands the section below it:

HTML:

<div class="expand">
    <div class="container1">
         <div class="container2">
             <a class="view">View</a>
             <a class="download">Download</a>
         </div>
    </div>
</div>

<section class="hidden">
...
</section>

jQuery:

$('.expand').click(function () {

   var $this = $(this);
   $this.next().show();

});

As you can see, there also a "Download" button as a child element of the <div class="expand">. This Download button should be the only element in this very container that does not trigger the said section to be shown.

So I would like to do something like this:

$('.expand').not(".download").click(function () {
...
});

or

$('.expand').except(".download").click(function () {
...
});
3
  • why not this $('.download').click(function () { Commented Apr 29, 2015 at 14:24
  • I think you misunderstood the question. I would like the whole <div class="expand"> container EXCEPT FOR the <a class="download"> button to trigger the function. Commented Apr 29, 2015 at 14:25
  • Do you want the event to not fire on click of download Commented Apr 29, 2015 at 14:26

5 Answers 5

5

You can use event.stopPropagation() as well:

$('.download').click(function(event) {
    event.stopPropagation();
}

$('.expand').click(function () {
    ...
});

This will stop the event from moving up in the DOM to parents, and triggering their event handlers as well.

Sign up to request clarification or add additional context in comments.

Comments

3

use e.target to check clicked element is download or not

$('.expand').click(function (e) {

    if (!$(e.target).is(".download")) {
        var $this = $(this);
        $this.next().show();
    }

});

3 Comments

just i posted .. i ll explain 2min
would make total sense to me, but it does not seem to work. It seems to keep the function from executing in the first place. Thanks though!
Then you have to use return if ($(e.target).is(".download")) { return; }else{}
2

You'll need to include a function at the end of the code:

It's here: event.stopPropagation()

Please try, it will work!

Comments

1

you can add stoppropagation() function after your code.

$('.expand').click(function (event) {   var $this = $(this);   $this.next().show();

event.stopPropagation(); });

Comments

1

You need event.stopPropagation(). This stops the event from bubbling or propagating to the children.

$('.expand').find(".download").click(function (event) {
     event.stopPropagation()
})

Comments

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.