0

I have something like the following

<div class='bar'>                
              <div class='container'>
                        <a class='add' href='#'>Add</a>
                        <a class='remove' href='#'>Remove</a>
             </div>
</div>

my jquery

  $('.bar').on('click', function(e){

     //do stuff A.   

 })

My question is how to prvent do stuff A when I click add and remove a tag in my case.

I want add and remove button doing nothing for now (eventually they will be a link though).

Thanks!

2
  • ...or remove the class from the HTML? But like @codehorse said, commenting it out would be the preferred method. Commented Dec 18, 2013 at 0:33
  • Not sure I understand your question completely. Maybe you're after preventDefault api.jquery.com/event.preventDefault Commented Dec 18, 2013 at 0:34

2 Answers 2

1

Prevent the propagation of event from both Add and Remove elements by calling .stopPropagation().

$('.container a').click(function(e){
    e.stopPropagation()
});

or inside the handler check whether the event originated from those elements like

$('.bar').on('click', function(e){
    if($(e.target).is('.container a')){
        return
    }

    //do stuff A.   
})
Sign up to request clarification or add additional context in comments.

Comments

0
$('.bar').on('click', 'a', function(e){
    return false;
});

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.