Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
113
rated 0 times [  114] [ 1]  / answers: 1 / hits: 16817  / 12 Years ago, mon, july 2, 2012, 12:00:00

I have a simple jQuery script that I'm trying to build upon but I can't get the href string comparison to return true:



<a class=test href=/Services/Cloud-Hosting>Click Me</a>​


My script is as follows:



$('.test').click(function() {
if ($(this).href == /Services/Cloud-Hosting) {
alert('hi');
}
else {
alert('no');
}
});​


I keep getting the alert of 'no' even thought the hrefs are the same. What am I missing?


More From » jquery

 Answers
49

Change:



if ($(this).href


To:



if (this.href


Or $(this).attr('href') but former is better.



To read attributes, you need to use attr (shorthand for attribute)



This is what you should have:



if (this.href == /Services/Cloud-Hosting) {
alert('hi');
}
else {
alert('no');
}

[#84513] Sunday, July 1, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
donaldcristianl

Total Points: 114
Total Questions: 95
Total Answers: 110

Location: Bonaire
Member since Sat, May 27, 2023
1 Year ago
;