Click here to Skip to main content
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 4-Apr-13 0:59am
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 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 85
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 4 Apr 2013
Copyright © CodeProject, 1999-2014
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