I am trying to find out if href attr is empty to do something, my code is as follows:
jQuery('#sidebar a').click(function() { var bob = jQuery(this).attr("href"); if(jQuery(bob).attr() == "") { alert('I am empty href value'); } });
I'm not sure where I am wrong? Any advice? Thanks!
You pass bob to jQuery as a selector. Just check it out directly:
bob
jQuery
jQuery('#sidebar a').click(function() { var bob = jQuery(this).attr("href"); if (bob == "") { alert('I am empty href value'); } });
Or better yet, simply:
if (!bob) {
Win-win live example
use this instead
jQuery('#sidebar a').click(function() { var bob = jQuery(this).attr("href"); if(bob === "") { alert('I am empty href value'); } });
The answer is already given by the great guys.
just check if (bob == "")
I would add another line. Just for security, you can crop bob with jQuery.
bob = jQuery.trim(bob);
This will make the action stronger.
jQuery('#sidebar a').click(function() { if(jQuery(this).attr("href")==""){ alert('I am empty href value'); } });
No need to filter your bob again using jQuery. :)
You set a variable and then do not check it. Personally, I would not even create a variable; Just check.
jQuery('#sidebar a').click(function() { if(jQuery(this).attr("href") == "") { alert('I am empty href value'); } });
You have already defined the href attribute for the bob variable. Just use:
href
bob == ""