Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selector for links with # in href attribute

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)

like image 247
simPod Avatar asked Dec 30 '11 19:12

simPod


People also ask

Which jQuery selector would you use to select all of the links in a page?

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.

What would be selector that you would use to select all hyperlinks whose href attribute's value ends with DOCX?

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”).

What are the jQuery selectors?

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.

Which of the following selects all h1 and h2?

:header Selector Selects all elements that are headers, like h1, h2, h3 and so on.


2 Answers

$("a[href*=#]").click(function(e) {     e.preventDefault();     alert('works'); });   
like image 83
epignosisx Avatar answered Sep 23 '22 00:09

epignosisx


*= 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 #

like image 41
Adam Rackis Avatar answered Sep 21 '22 00:09

Adam Rackis