Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery How to test if this <a> has href attribute

Very simple question...

for

$('a')

How do I test if that jQuery object has an HREF attribute? I'm Looking for something that will return true or false that I can use in an if() statement.

Thanks!

like image 863
HandiworkNYC.com Avatar asked Dec 08 '25 18:12

HandiworkNYC.com


2 Answers

jQuery's .attr() function will return undefined if the attribute to be found is missing in the selected element(s).

if($('a').attr('href') === undefined) { 
   // Element 'a' has no href
}

Note: There are other ways to test for undefined.

like image 97
A Person Avatar answered Dec 10 '25 21:12

A Person


if ($('a').not($('a:link')).length) {
    //you have `<a>` elements that are placeholders only
}
like image 25
adeneo Avatar answered Dec 10 '25 22:12

adeneo