How to get element by href in jquery?

jQuery is one of the most popular and widely used JavaScript libraries in the world. Its beauty is in its selectors and inbuilt functions which enable you to perform many complex tasks with very few lines of code.

While working on client’s project, I was required to select <a> tag element by its URL i.e value of href attribute. Thanks to jQuery, I was able to perform this task very easily. Here’s the code which allows you to select <a> element by href.

var link = $('a[href="http://coffeecupweb.com"]');

The above code will select <a> element with http://coffeecupweb.com. You can also select links starting with a certain URL using attributeStartsWith selector.

var links = $('a[href^="http://coffeecupweb.com"]');

See Also:

Check if an image is loaded using jQuery

0 Comments

Leave a Comment.