Reputation: 29
I'll explain it best I can. I have the buttons (Red Ones Right side) in JavaScript so when I click on each one they turn green and when I click them again they turn red. I'm trying to get the image button (Left side) to work where if I click each one it will turn the JavaScript buttons (Red Ones Right side). Meaning I can use either one to enable/disable.
Here is image:
Html code:
<img src="images/menuIcons/skip.gif" width="126" height="34" />
<div align="center" style="margin-bottom: 10px; margin-left: 10px;">
<button id="skip" class="red" onclick="controls(this.getAttribute('class'),this.id)"></button>
<img src="images/menuIcons/text.gif" width="126" height="34" />
<div align="center" style="margin-bottom: 10px; margin-left: 10px;">
<button id="text" class="red" onclick="controls(this.getAttribute('class'),this.id)"></button>
<img src="images/menuIcons/message.gif" width="126" height="34" />
<div align="center" style="margin-bottom: 10px; margin-left: 10px;">
<button id="message" class="red" onclick="controls(this.getAttribute('class'),this.id)"></button>
<img src="images/menuIcons/game.gif" width="126" height="34" />
<div align="center" style="margin-bottom: 10px; margin-left: 10px;">
<button id="game" class="red" onclick="controls(this.getAttribute('class'),this.id)"></button>
Javascript code:
// enable / disable Buttons
function controls(className,elem) {
if (className == "red") {
document.getElementById(elem).setAttribute('class','green');
// You can define your enable statements here
} else {
document.getElementById(elem).setAttribute('class','red');
// You can define your disables statements here
}
}
Upvotes: 1
Views: 807
Reputation: 122906
It's all a bit complex. You could surround your menu with a container and use event delegation to add a click handler for all items. Here is a (plain javascript) jsfiddle mockup for your menu.
Some notes on your html/code:
id
, that's asking for trouble. id
's should
be unique.Upvotes: 0
Reputation: 664548
You can just call the function from an onclick
on the image, too. However, I would recommend to restructure the controls
function to take only the id
as an argument then.
<img src="images/menuIcons/skip.gif" onclick="controls('skip')" width="126" height="34" />
...
<button id="skip" class="red" onclick="controls(this.id)"></button>
<!-- the other elements analogous -->
// enable / disable Buttons
function controls(elemid) {
var elem = document.getElementById(elemid);
if (elem.className == "red") {
elem.setAttribute('class', 'green');
// You can define your enable statements here
} else {
elem.setAttribute('class', 'red');
// You can define your disables statements here
}
}
Upvotes: 1