0

I am trying to enable and change the background color of a (specific) set of buttons. How would I go about grouping these buttons and calling them so I do not need to set a specific Id for each?

EDIT:

To give more insight into what I am trying to do. Buttons are all laid out as such:

<button onclick="keyPress('e')">e</button>

What I tried doing(also tried Id and Name):

//light up keys
//document.getElementsByTagName("button").style.backgroundColor = 'silver';

//activate keys
//document.getElementsByTagName("button").disabled = false;
  • I'm assuming the asker is trying to change the background of the buttons on the fly, not have them set by default. – Snowburnt Oct 02 '13 at 16:55
  • Snowburnt is correct, I should have been more specific. – Michael McCormick Oct 02 '13 at 17:05
  • The best way to group elements is by classes, you'll have to assign the same class to buttons that you want grouped together. With no other attributes being assigned to your buttons there's absolutely no way for javascript to know how to differentiate them, unless you do something random like assign the color to every other button in the collection – Snowburnt Oct 02 '13 at 17:35

4 Answers4

0

use jquery...

$("button").css("background", new color)

if you want groups of buttons use the class attribute:

$("button.class1").css("background", new color)

for javascript:

var buttons=getElementsByTagName("button");
for (var i = 0; i < buttons.length; i++) { 
   buttons[i].style.backgroundColor="newcolor";
}

here's a way to do it by class name so that you can group elements by class with pure javascript: How to Get Element By Class in JavaScript?

to enable buttons set the element button element .enabled=true or false

Community
  • 1
  • 1
Snowburnt
  • 6,523
  • 7
  • 30
  • 43
0

If we're talking pure javascript:

Select them into an array with var array = document.getElementsByTagName("button"); and manipulate within a loop. You can either set them to a css class using .className = "whatever" or you can manually set it using the .style = "whatever" object

Loop to check className:

for (i=0; i<array.length; i++) {
    if (array[i].className == [whatever condition]) {
        array[i].style.backgroundColor = "whatever"; 
        //or array[i].className = "whatever"
    }
}
Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
0

Create a "css class" and apply the same class to all the button.

<style>
.btn-color
{
   background-color:grey;
}
</style>

<input type="button" id = "btn1" class = "btn-color" />
<input type="button" id = "btn2" class = "btn-color" />
<input type="button" id = "btn3" class = "btn-color" />
<input type="button" id = "btn4" class = "btn-color" />

For Dynamically , then using JQuery

 $(".btn-color").css("background-color","grey")

Using Javascript

var elements = document.querySelectorAll(".btn-color");
for (var i = 0; i < elements.length; i++) {
    elements[i].style.backgroundColor = "blue";
}

JSFiddle : http://jsfiddle.net/EmzH8/

Viji
  • 2,629
  • 1
  • 18
  • 30
-1

if you are searching for code to dynamically change background of a set of buttons try this:

css:

.myClass{
background-color:red;
}

js:

function changeBG(){
var inputs=document.getElementsByTagName("input")
var buttons=document.getElementsByTagName("button")

for(var i=0;i<inputs.length;i++){
var input=inputs[i]
if(input.type=="button"){
input.className="myClass"
}
for(i=0;i<buttons.length;i++){
var button=buttons[i]
button.className="myClass"

}

}

remember, this code will change background of all the buttons in your page

Nishad K Ahamed
  • 1,374
  • 15
  • 25