I am trying to find out if a href attr is empty 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 am not sure where I am going wrong? Any advice? Thanks!
You're passing bob
into jQuery
as a selector. Just test it directly:
jQuery('#sidebar a').click(function() {
var bob = jQuery(this).attr("href");
if (bob == "") {
alert('I am empty href value');
}
});
Or better yet, just:
if (!bob) {
Gratuitous live example
You are setting a variable and then never checking it. Personally, I wouldn't even create a variable; Just do the check.
jQuery('#sidebar a').click(function() {
if(jQuery(this).attr("href") == "") {
alert('I am empty href value');
}
});
use this instead
jQuery('#sidebar a').click(function() {
var bob = jQuery(this).attr("href");
if(bob === "") {
alert('I am empty href value');
}
});
Answer is already given by great guys.
just check if(bob=="")
I would add one more line. Just for safety you can trim bob using jQuery.
bob = jQuery.trim(bob);
This will make the validity a bit stronger.
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