0

I have this simple line of code:

document.getElementById('tb').style.background = '#FFFFFF';

My CSS stylesheet is:

.tb {
    background: #2f2f2f;
}

Why isn't my Javascript code working?

I'm new to Javascript so if you could please explain the problem rather than tell me the problem, I'd be grateful!

2
  • 2
    Uhm, id or class? .td != #td Commented Jul 17, 2014 at 20:00
  • @elclanrs Sorry. Class. How should I do this with a class? Commented Jul 17, 2014 at 20:07

6 Answers 6

2

Your element use a class, not an id.

Change it for :

<div id="tb">Foo bar</div>

And :

#tb {
    background: #2f2f2f;
}

If you wanna keep using a class, use this JS instead :

// Get all .tb elements
var tbs = document.getElementsByClassName('tb')
i = tbs.length;
// Loop
while(i--) {
    // Change their background
    tbs[i].style.backgroundColor = "#FFFFFF";
}

Or, if you are just interested by the first element :

document.getElementsByClassName('tb')[0].style.background = "#FFFFFF";
Sign up to request clarification or add additional context in comments.

5 Comments

Right. OK. I need to keep it as a class. How can I change the background property of a class via Javascript?
Do you mean document.getElementsByClassName('tb')[0].style.background = "#FFFFFF";? Instead of backgroundColor? Either way, it still doesn't work...?
It does work. And you can use background or backgroundColor, whatever (I changed it to fit your original post).
it works there, but I get the error Cannot read property 'style' of undefined. I literally copied and pasted it.
That mean you don't have any .tb element in your document.
0

The main problem here is that like zessx said, tb is apparently a class, not an ID (if I assume your CSS stylesheet is correct).

For your CSS stylesheet, when you're using the

.something {
}

syntax, the period means it's a class. ID's would be referred to by using

#something {
}

To see which one is actually going wrong, we'll probably need to see some more code where 'tb' is used.

And in the case that 'tb' is actually a class, you should potentially try using jQuery, where you can get elements of a certain class.

2 Comments

Is there no Javascript technique? I only just started it and I thought this was a simple starter!
I mean if you want to do it using javascript, one thing you could do is just also put an id tag wherever this class is, and then use the method you were using above. You also have var elements = document.getElementsByClassName('someClass'); but this does not work in some browsers, like IE 8 I believe.
0

Check the below fiddle link

Here is your html code

<div id="tb">&nbsp;</div>

JSFiddle link

Regards Maha

2 Comments

I need to keep it as a class. How do I do this with a class??
change your code as document.getElementsByClassName('tb').style.background = 'red';
0

Yeah, as others have stated you're not referencing an id. Super quick fiddle to show you how to reference the class with js.

var a = document.getElementsByClassName('tb'); a[0].style.background = '#FFFFFF';

1 Comment

Everyone on this post has supplied a valid answer that should work for you. If you still can't get it to work you should post the full code so we can take a look.
0

To get your element and change it style use document.getElementsByClassName('tb')[0].style.background = '#FFFFFF'. Fiddle: http://jsfiddle.net/vvRs2/.

Comments

0

if your css looks like this:

.tb {
    background: #2f2f2f;
}

Than "tb" is a class( "." is the CSS selector for a class). What you need to do is make "tb" an id and than it will work.

Example: HTML:

<div id="tb">whatever</div>

CSS:

#tb {
    background: #2f2f2f;
}

Or you can replace your JS with:

document.getElementByClassName('tb').style.background = '#FFFFFF';

but i would recommend the first method. Hope it helps.

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.