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) {
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.
jQuery('#sidebar a').click(function() {
if(jQuery(this).attr("href")==""){
alert('I am empty href value');
}
});
There is no use of filtering your bob again with jQuery. :)
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');
}
});
You are defining the attribute href
to variable bob
already. Just use:
bob == ""
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.