I have some javascript that is supposed to run after the window loads but for some reason, it never runs.
Here's my code:
function setClasses(){
document.getElementsByClassName("gchoice_35_0")[0].onclick = sedanShow;
document.getElementsByClassName("gchoice_22_0")[0].onclick = sedanShow;
document.getElementsByClassName("gchoice_34_0")[0].onclick = sedanShow;
document.getElementsByClassName("gchoice_34_1")[0].onclick = suvShow;
document.getElementsByClassName("gchoice_35_1")[0].onclick = suvShow;
document.getElementsByClassName("gchoice_22_1")[0].onclick = suvShow;
document.getElementsByClassName("gchoice_22_2")[0].onclick = vanShow;
document.getElementsByClassName("gchoice_35_2")[0].onclick = vanShow;
document.getElementsByClassName("gchoice_34_2")[0].onclick = vanShow;
}
window.onload = setClasses;
The setClasses() function doesn't seem to run. It does however work when I manually call it through the console of FireBug.
The code is placed in the header of my web page.
Any help is appreciated.
Full html snippet:
<head>
......
<script type="text/javascript">
function setClasses(){
document.getElementsByClassName("gchoice_35_0")[0].onclick = sedanShow;
document.getElementsByClassName("gchoice_22_0")[0].onclick = sedanShow;
document.getElementsByClassName("gchoice_34_0")[0].onclick = sedanShow;
document.getElementsByClassName("gchoice_34_1")[0].onclick = suvShow;
document.getElementsByClassName("gchoice_35_1")[0].onclick = suvShow;
document.getElementsByClassName("gchoice_22_1")[0].onclick = suvShow;
document.getElementsByClassName("gchoice_22_2")[0].onclick = vanShow;
document.getElementsByClassName("gchoice_35_2")[0].onclick = vanShow;
document.getElementsByClassName("gchoice_34_2")[0].onclick = vanShow;
}
function sedanShow(){
document.getElementById("sedan").style.display="inline"
document.getElementById("suv").style.display="none"
document.getElementById("van").style.display="none"
}
function suvShow(){
document.getElementById("sedan").style.display="none"
document.getElementById("suv").style.display="inline"
document.getElementById("van").style.display="none"
}
function vanShow(){
document.getElementById("sedan").style.display="none"
document.getElementById("suv").style.display="none"
document.getElementById("van").style.display="inline"
}
window.onload = setClasses;
</script>
......
You could always use JQuery
Edit
Example JQuery Event Refactor:
I am going to give an answer that uses YUI, feel free to use it or not, but I think frameworks are a good idea to help speed up javascript development. So, add the following in to your page
Then instead of window.onload = someFn (because this can be quirky in IE, surprise surprise), do the following
Then in your set classes function, do the following
That should do the trick.