I tried to use this jQuery selector:
$("a:has(href*=#)").click(function() { alert('works'); });
but it doesn't seem to work. I would like to select all tags which have anchor in href attribute (has # symbol there)
To select all links inside paragraph element, we use parent descendant selector. This selector is used to selects every element that are descendant to a specific (parent) element.
Similar to the “begins with” selector, the “ends with” selector allows for the selection of elements where a specified attribute (e.g. the href attribute of a hyperlink) ends with a specified string (e.g. “. pdf”, “. docx” or “. mp3”).
jQuery selectors allow you to select and manipulate HTML element(s). jQuery selectors are used to "find" (or select) HTML elements based on their name, id, classes, types, attributes, values of attributes and much more. It's based on the existing CSS Selectors, and in addition, it has some own custom selectors.
:header Selector Selects all elements that are headers, like h1, h2, h3 and so on.
$("a[href*=#]").click(function(e) { e.preventDefault(); alert('works'); });
*=
will filter attributes that contain the given string anywhere
$("a[href*='#']").click(function() { alert('works'); });
Also note that
$("a[href^='#']").click(function() { alert('works'); });
will select any anchor whose href starts with a #
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With