I am using jQuery for the first time to add a class to an element within the navigation bar, but the class, ‘.selected’ isn’t being added.
Please can you tell me where I am going wrong?
Here is an example: http://jsfiddle.net/v32qy/2/
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
thisis not a jQuery object, it’s the actual DOM element. To turn it into a jQuery object, use$(this). So:Another problem in the jsFiddle is that you’re not selecting the
<a>element, you’re selecting the<h2>. Finally, the link is being followed. Here’s all that fixed.Something else you might consider is that
element.innerTextdoesn’t have very good browser support from what I’ve seen (i.e. it only works in IE). Just a small point of note.