Click here to Skip to main content
11,701,953 members (62,097 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript
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
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 3-Apr-13 23:59pm
Comments
enhzflep at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hyphenated CSS attributes have to be converted into camel case when using 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)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

"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:

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';
       }
   }
  Permalink  
v2

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

  Print Answers RSS
0 Maciej Los 420
1 Sergey Alexandrovich Kryukov 355
2 OriginalGriff 314
3 Mika Wendelius 119
4 CHill60 105
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,507
2 CPallini 4,922
3 Maciej Los 4,371
4 Mika Wendelius 3,556


Advertise | Privacy | Mobile
Web02 | 2.8.150819.1 | Last Updated 4 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100