views:

63

answers:

4

I have a <input> element that I want to change the background color on. The code I am currently using is this (but it is not working):

var allBlanksLoc = document.getElementById('text');
var allBlanks = allBlanksLoc.getElementsByTagName('input');

for(i=0; i<allBlanks.length; i++) {
var currentBlank = allBlanks[i];
var wordNum = blanks[i];
var blankWord = text[wordNum];
var usrAnswer = currentBlank.value;

if (usrAnswer != blankWord) {
currentBlank.style.backgroundColor = "red";
}
}

The third to last line being the most important

Update:

I fixed the camelCase on it but it still does not work. Any ideas of bugs there?

The full code is here: http://jsbin.com/imolo3/edit

+2  A: 

It should be backgroundColor - notice the capital C, JavaScript is case-sensitive.

casablanca
or `style['background-color']`
Felix Kling
+1  A: 

Are you sure that this script is running at the right time? If it runs before the page is fully formed, the appropriate elements might not be present.

Tim
yeah, its running at the right time. check out the code: http://jsbin.com/imolo3/edit
chromedude
OK, you have other problems with your code. `text` is undefined at the point where it is needed, as it is initialised as a local variable within another function. If you are using Firefox, have a look in the Error Console to diagnose this sort of thing.
Tim
ah... I forgot about that being localized. Thanks. I was looking at the chrome console and it wasn't showing that one. I probably should have used firebug.
chromedude
@Tim do you have any ideas on how to make text non localized? I need it not to be created until the createFIB() function is run but I need access to it in all other functions
chromedude
Go on, I'm in a generous mood. How about a deal. I fix your code, summarise what I've done in this answer, and you vote the answer up and accept it. :-)
Tim
@Tim sorry, didnt see your comment. I fixed it now, though I was going to vote you up and put your answer as the answer.
chromedude
Fine by me. :-) Actually, your randomisation routine isn't very efficient, and can sometimes fail to terminate. :-( I'll give you some other ideas - do take them or ignore them as you see fit!
Tim
@Tim thought you would not mind :) . Also I just updated that jsbin to the latest code. The color validation at the end does not always work have any ideas on that?
chromedude
@Tim actually the new version is here http://jsbin.com/imolo3/3
chromedude
OK, have a look here: http://jsbin.com/imolo3/4/edit
Tim
@Tim looks kind of good. The validation is not working at all. It is saying that everything is wrong when it really is right.
chromedude
Hmmm... Can you paste here what you're putting into the top box as an example of something where it goes wrong?
Tim
I put "hello how are you doing. What is going wrong? I do not know." (that is this time I put that. Last time I put a different string.
chromedude
Whoops. Yeah, that's pretty messed up. Sorry. Give me a sec.... :-(
Tim
OK, try this: http://jsbin.com/imolo3/5/edit
Tim
Still having problems. Looks like the input node list is offset by one because it is saying I am wrong and I should have typed the word that is for the next input element.
chromedude
Ah, sorry. It was working fine for the first time after the page loaded, but wasn't reinitialising itself when you rebuilt the test without reloading the page. Try this: http://jsbin.com/imolo3/6/edit
Tim
@Tim It looks great! Thanks so much! Now I have to go through the code to see what you improved/changed so that I can learn from it.
chromedude
@Tim I was looking through the code. I understand what is happening but in some cases do not understand how you made it happen with the code you used. Do you think we could talk over email? Get my email by going to this link: http://goo.gl/GX3U (a reCAPTCHA)
chromedude
@Tim, Just in case you didn't see (or my email went into your spam folder) I sent you the email. Its no problem I was just making sure that you received it.
chromedude
Yes, have done. Give me a day or two to get my act together. :-)
Tim
No problem, just making sure.
chromedude
A: 

Case is important. What you need is

document.getElementById('test').style.backgroundColor='red';

However

it would be better to use a css rule and use javascript only to add the class to the element.

CSS Rule

input.invalid {
    background-color: red;
}

Javascript

element.className = 'invalid';
bemace
A: 

So not to repeat the solutions other users gave.

I personally use JQuery (and it's where any javascripter ends, overall for browser compatibility issues), and it would:

$(currentBlank).css("background-color","red");
netadictos