I have two button classes that essentially are just 'off' and 'on' images. When a button is pushed, the class of the button switches. Modifying the code im using to turn single buttons on and off, I want to turn one button off when another is turned on, but my getElementByName never seems to work. Is there a better approach?
HTML Buttons:
<input type="button" id="avgbtn" value="2015 Season Avg" class="offgo" onclick="toggleStateavg()" />
<input type="button" id="tradebtn" value="2015 Trade Deadline" class="ongo" onclick="toggleStateavg()"/>
Javascript:
var a = document.getElementById("avgbtn");
var b = document.getElementById("tradebtn");
function toggleStateavg(){
if(a.className == "ongo") {
a.className = "offgo";
b.className = "ongo";
avg = "one";
} else {
a.className="ongo";
b.className = "offgo";
avg = "two";
}
}
getElementsByName. Those elements have ID attribute, use thegetElementByIdinstead.getElementByIdnotgetElementByID. JavaScript is case-sensitive!getElementByIDsince it's a bit quicker as it only searches for one occurrence of ID whereas you can get a list of names