1

I want to change this line:

    <li id="activehome" ><a href="#" >Home</a></li>

to

    <li id="activehome" class="active"><a href="#" >Home</a></li> 

Adding that class attribute. How do I do this using Javascript or even Jquery will do. Thanks in advance. The line should be changed when the page loads.

2
  • Show what did you try so far and others will help you to fix it. Commented Sep 6, 2015 at 5:06
  • Have you tired anything or simply asking your question without trying anything ? show what you tired Commented Sep 6, 2015 at 5:19

2 Answers 2

1

jQuery, using addClass:

$('#activehome').addClass('active');

pure JS, using setAttribute:

document.getElementById('activehome').setAttribute('class', 'active');

or using className

document.getElementById('activehome').className = 'active';
Sign up to request clarification or add additional context in comments.

4 Comments

I have tried this. It doesn't work. The idea behing it is I have a navigation bar which I call in different pages. So I have put the code for navigation bar in a file nav.php and called it using include in my index.php. I want to use JS or JQuery to change the Home button to active or for any button on any page where I call this nav bar. If there is a better solution please suggest me that.
Yes it works according to the information you have given. Please update your question with enough information for us to answer this for your situation - My assumption is you have multiple IDs with activehome
What do you mean by 'doesn't work'? You need to be more specific.
Hey sorry! It worked after I wrote it inside onload. Thank you :)
0

$("#activehome").addClass('active'); $("#activehome > a").text('Home');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.