0

I'm building a form that changes the background color of certain elements based on choices made. Later on I have the form validate based on static numerical values, but want to create exceptions for any fields that have had their background color changed.

My thought is collecting these elements into an array based on background color would work. I'm not sure how to check for a background color though. Hopefully the code below shows my thought process. The condition in the if statement doesn't work, I don't know if there's something similar that might pull this off.

var group = document.getElementsByClassName = ('groupClass');
for(i=0;i < group.length;i++){
  if(group[i].style.backgroundColor == "#000"){
    //add group[i] to an array
  }
}
2
  • How is the style applied via css or inline? Commented Apr 12, 2013 at 20:50
  • CSS. I tried creating two variables, one for hexadecimal and another for RGB and then checking for either color, but it didn't work very well. I already had a css class attached to my elements which is why at first I didn't want to go this route, but ended up attaching a new class with the old class. Commented Apr 16, 2013 at 15:55

2 Answers 2

3

Instead of changing the background color, give it a class that changes the background color, then look for that class.

There are a couple problems in your code though.

var group = document.getElementsByClassName = ('groupClass');`

should be

var group = document.getElementsByClassName('groupClass');

Also, try comparing with 'rgb(0, 0, 0)' instead of '#000'. (That works in at least Chrome.) See http://jsfiddle.net/evrk4/.

Sign up to request clarification or add additional context in comments.

2 Comments

Yep, I didn't notice her error there. I should have tested it first.
Oh, duh. I typed up an example real fast, this isn't my code, but thanks for pointing it out!
0

The proceeding line is an issue.

var group = document.getElementsByClassName = ('groupClass');

Its a method call it should be:

var group = document.getElementsByClassName('groupClass');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.