views:

41

answers:

1

Hello, I have a piece of javascript code like this right now.

document.getElementById(pos).style.backgroundColor='#800080';

I want to refer to the following gradient background color instead of a staic value '#800080' in the code.

.myBK {background: -moz-linear-gradient(top,  #ccc,  #000);}

What is the correct syntax to do that?

I tried this but it does not work.

document.getElementById(pos).style.backgroundColor=.myBK;

Thanks a lot.

+1  A: 

You can either add the element to the class myBK:

document.getElementById(pos).className += " myBK";

Or you assign the value to the background property:

document.getElementById(pos).style.background = "-moz-linear-gradient(top, #ccc, #000)";
Gumbo
What’s wrong? What’s the reason for the down vote?
Gumbo
> *What’s the reason for the down vote?* You had `.class += ...` first.
Roatin Marth
Hello,If I can't use -moz-linear-gradient to render a color, what should I use otherwise? Can I use -webkit-gradient? I don't have an image. I just want to render a gradient color. I tried this code but it still does not work. Thanks for your help....<script language="JavaScript" type="text/javascript">function selectedFocus(pageSize, pos){ for(var s=1; s<pageSize+1; s++){ if(pos == s){ document.getElementById(pos).className += 'myBK'; } }}</script><style type="text/css">.myBK{-webkit-gradient( linear, center bottom, color-stop(0.17, rgb(243,95,62)))}
Java Doe
@Java Doe: The space in front of `myBK` in `" myBK"` is important as multiple class names are separated by one or more white space characters.
Gumbo
hello, Gumbo:I am sorry to bother you again.I added a blank space but that still did not help.Is this because -webkit-gradientis not the right way to go?Do I need to add a . in front of myBK or I should not need a . there.myBK{ -webkit-gradient( linear, center bottom, color-stop(0.17, rgb(243,95,62)) ) }Thanks again.
Java Doe
@Java Doe: IDs cannot begin with a digit. See http://www.w3.org/TR/html4/types.html#type-id
Gumbo