0

I need to change the Div id "Div01" state to "highlight" class when the page loads.
Please see the code below:

HTML:

<div id="div01"><a href="#" class="btn" onclick="roleActive(this);">user</a></div>

JS:

var role; function roleActive(obj){ if (role)
role.className = 'btn';
obj.className = 'highlight';
role = obj;}

CSS:

.btn{
display:block;
height:25px;
width:100px;
padding:5px 0px 0px 0px;
text-align:center;
font-size:16px;} 

.highlight{
cursor:default;
display:block;
height:25px;
width:100px;
padding:5px 0px 0px 0px;
text-align:center;
font-size:16px;
background-color:#FFF;
border-bottom:4px solid #F0AB00;
color:#000;
text-decoration:underline;}

a.btn:link{background-color:#000;color:#FFF;}
a.btn:visited{background-color:#000;color:#FFF;}
a.btn:hover,active{background-color:#FFF;color:#000;text-decoration:underline; border-bottom:4px solid #F0AB00;}

Kindly help me to fix the problem.

5 Answers 5

2

Try

<script>
    window.onload = function(){
        roleActive(document.getElementById('div01').children[0]);
    }
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much. It really worked for me :).<br> I have one more query but it hard to explain here. How can i create it using the fiddle.net
Hi @Arun, Please find the fiddle here : jsfiddle.net/aki_shaitan/ruDek. <br/> In this i want a function that when i click on "Button 01" it should open its submenu"01". After this when click on some other button, it should it hide the previous submenu and open the relevant submenu.
@Ankit it will be better if you post it as a separate question'
I am facing some issue with this script because i made some changes in the functionality. Kindly help me.
1

try this

window.onload = function(){
    document.getElementById('div01').className = 'highlight'
} 

or

<script>
 document.getElementById('div01').className = 'highlight'
</script>

or

<script>
exampleCall();
function exampleCall()
{
 document.getElementById('div01').className = 'highlight'
}
</script>

1 Comment

using this function, the class "highlight" gets changed but still i can see the ".btn" class on top.
1
document.addEventListener('DOMContentLoaded', function(){
    document.getElementById('div01').className = 'highlight';
}, false); 

Comments

0

For Javascript:

window.onload = function() {
        document.getElementById('div01').className = 'highlight';
}

For Jquery:

$(function() {
  $('#div01').addClass('highlight');
});

Comments

0

Your onclick event is sending 'this' to the function, but it's the onclick event of the , not of the . Is this what you intended?

Since you're not using the inherent click functionality of the then I'd suggest getting rid of it. Just have the click event on the , so clicking it will fire roleActive() and apply the classes appropriately.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.