I am using event.preventDefault() to prevent concatenation of # which is the href of an anchor to the URL. I am performing events on the mousedown() and mouseup() parts of the click which is why I can’t use click. But event.preventDefault() is not preventing the concatenation of # to the URL when envoking mouseup() or mousedown() methods. How can I get around this?
I am using event.preventDefault() to prevent concatenation of # which is the href of
Share
If you’re talking about clicking a link, it is probably because there isn’t a default behavior to prevent for
mousedownandmouseup.The default behavior of clicking a link requires a combination of
mousedownplusmouseupon the link. If youmousedownthen drag off the link before youmouseup, the link is not followed. The same vice-versa.Only when you
mousedownthenmouseupis the default behavior activated. That event is represented by theclickevent.EDIT: I guess I forgot to answer the question.
How do you get around it? Add a
click()event handler that doese.preventDefault().If you also want to stop propagation of the event, and if you’re using jQuery 1.4.3 or later, you can do this:
From the docs for the
bind()(docs) method:Again, it requires
jQuery 1.4.3or later.