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
Try
<script>
window.onload = function(){
roleActive(document.getElementById('div01').children[0]);
}
</script>
answered Apr 19, 2013 at 3:22
Arun P Johny
389k68 gold badges533 silver badges532 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
Ankit
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
Ankit
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.
Arun P Johny
@Ankit it will be better if you post it as a separate question'
Ankit
I am facing some issue with this script because i made some changes in the functionality. Kindly help me.
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>
answered Apr 19, 2013 at 3:22
PSR
40.4k43 gold badges115 silver badges154 bronze badges
1 Comment
Ankit
using this function, the class "highlight" gets changed but still i can see the ".btn" class on top.
document.addEventListener('DOMContentLoaded', function(){
document.getElementById('div01').className = 'highlight';
}, false);
answered Apr 19, 2013 at 3:22
Alex
35.5k5 gold badges81 silver badges94 bronze badges
Comments
For Javascript:
window.onload = function() {
document.getElementById('div01').className = 'highlight';
}
For Jquery:
$(function() {
$('#div01').addClass('highlight');
});
answered Apr 19, 2013 at 3:24
user1526546
Comments
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.
answered Apr 19, 2013 at 3:48
David Gilbertson
4,9341 gold badge32 silver badges34 bronze badges
Comments
lang-js