Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i want to write a function in javascript which will compare the value from my drop down and for different values..i need to color the bg color of that cell.

I tired something like this
C#
function colorValue()
{
    var activeCompletedOnHold = form.active_Completed_OnHold.value;
   
    if(activeCompletedOnHold=="Active1")
    {
        this.style.background-color=#347C17;
    }
    else if(activeCompletedOnHold=="Completed")
    {
         //this.style.background-color='#347C17';
    }
    else if(activeCompletedOnHold=="On Hold")
    {
    //this.style.background-color='#FF0000';
    }
}



Please let me know what is wrong in this.
Posted
Comments
enhzflep 4-Apr-13 6:48am    
Unless it's a hand-rolled drop-down, you can't (reliably in a cross-platform manner).
I've assumed your 'drop-down' is a <select> list and that the elements you'd like to style are <option> elements.

Hyphenated CSS attributes have to be converted into camel case when using Javascript:
JavaScript
function colorValue()
{
    var activeCompletedOnHold = form.active_Completed_OnHold.value;

    if(activeCompletedOnHold=="Active1")
    {
        this.style.backgroundColor='#347C17';
    }
    else if(activeCompletedOnHold=="Completed")
    {
         //this.style.backgroundColor='#347C17';
    }
    else if(activeCompletedOnHold=="On Hold")
    {
    //this.style.backgroundColor='#FF0000';
    }
}


You do need quotes around the colour value too.

(This is assuming that this is an element that supports a background-color style)
 
Share this answer
 
"this" refers to the window object.
and when you are changing style property you cannot use dash(-) instead you write the properties using cammel case, so:

background-color will be backgroundColor

Do you want to change the color of a specific element when your dropdown value is changed?
Is that specific element the "parent" of the dropdown or is another "random" element?

if what you want is change the dropdown background color, for example, your function could be:

JavaScript
function colorValue() {
       var activeCompletedOnHold = form.active_Completed_OnHold.value;

       if(activeCompletedOnHold==="Active1")
       {
           form.active_Completed_OnHold.style.backgroundColor="#347C17";
       }
       else if(activeCompletedOnHold==="Completed")
       {
           form.active_Completed_OnHold.style.backgroundColor='#0000FF';
       }
       else if(activeCompletedOnHold==="On Hold")
       {
           form.active_Completed_OnHold.style.backgroundColor='#FF0000';
       }
   }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900