I’m using this jQuery selector:
$("a[href*='#']").click(function(e) {
e.preventDefault();
alert(#valueinhere)
});
to select links that link to an anchor
My links are absolute and relative so they can look like http://www.myweb.com/test#anchor or like test#anchor or like #anchor
How to get the string #anchor from all links above? Is there any regex or something like that?
(I can’t use window.location.hash because of e.preventDefault() that is necessary)
Why can’t you get the
hashfrom the anchor element?It is independent of whether you’re preventing the default behavior of the click event.
Here’s a working example.