0

I have this markup,

<a href="http://www.example.com">Hiya<span class="delete">Delete</span></a> 

And my js,

$('.delete').click(function() { window.location = 'http://www.example.com/example'; }); 

My problem the anchor tag always win presedence over my javascript window.location how can I overcome this?

Thanks

1
  • 1
    Why place it inside an <a> tag if you dont want that functionality. Why not <a href="http://www.example.com">Hiya</a><span class="delete">Delete</span>? Commented Dec 16, 2011 at 12:46

4 Answers 4

4

You need to return false from the click() event to prevent the default behaviour:

$('.delete').click(function(e) { e.stopPropagation(); window.location = 'http://www.example.com/example'; return false; }); 
Sign up to request clarification or add additional context in comments.

5 Comments

This doesn't work as it is in its own span tag and not the anchor tag!
Add e.stopPropagation() to stop the event bubbling up to the <a>. I have edited my answer for you.
Thanks, will have to look up on that it works though thanks!!
Thanks. If it helped, would you mind accepting the answer? :)
Had to wait there is a time limit now
1

Use return false;

$('.delete').click(function() { window.location = 'http://www.example.com/example'; return false; }); 

Comments

0

Have you tried with:

$('.delete').click(function(ev) { ev.preventDefault() window.location = 'http://www.example.com/example'; }); 

Not sure, though, since "span" is not a clickable tag.

1 Comment

It's clickable, it just doesn't have a default action to prevent. ev.stopPropagation(); should work though, to stop the click bubbling up to the anchor element.
0

You might be looking for http://api.jquery.com/event.stopPropagation/

$(".delete").click(function(event){ event.stopPropagation(); // do something }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.