setAttribute question

Hello all. trying to get more experience w/javascript.

why does this work.

var myScratch = document.getElementById("scratch");
myScratch.setAttribute("align", "right");

but not this.

var myScratch = document.getElementById("scratch");
myScratch.setAttribute("background", "#ffff00");

thank you all!
D

Maybe try adding “-color”

As Mitteneague has pointed out:


var myScratch = document.getElementById("scratch");
myScratch.setAttribute("background[COLOR=#0000ff][B]-color[/B][/COLOR]", "#ffff00");

If you are going to set an attribute you need to set the entire attribute and not just a part of it. The background attribute requires a number of values in order to set it properly and you only provided a small part of what it needed.

Nope, & actually i had already tried.

var myScratch = document.getElementById("scratch");
myScratch.setAttribute("background-color", "#ffff00");

I was also thinking that the attribute -color is not always strictly necesary. as you can add a background: url(‘image/whatever.jpg’), #ffff00; and have both an image & color.
& also tried

var myScratch = document.getElementById("scratch");
myScratch.setAttribute("color", "#ffff00");

to see if i could affect the color of the font. still no dice

Ah… I keep forgetting… JS doesn’t use the exact same format as CSS… try backgroundColor.

ok thanks all.
I got the answer from one of my more experienced collegues. Sharing in case it helps:
Original was not working because “Because align is both an element attribute and a css attribute but backgroundColor isn’t…”

myScratch.setAttribute('style','background-color:#ffff00;')

is correct and will change the color.