Click here to Skip to main content
11,492,523 members (62,938 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 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 Sergey Alexandrovich Kryukov 404
1 Maciej Los 266
2 KarstenK 220
3 Sascha Lefèvre 215
4 CPallini 208
0 Sergey Alexandrovich Kryukov 9,943
1 OriginalGriff 8,610
2 Sascha Lefèvre 3,609
3 Maciej Los 3,372
4 Richard Deeming 2,440


Advertise | Privacy | Mobile
Web03 | 2.8.150520.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