Results 1 to 8 of 8

Thread: SELECT onclick

  1. #1
    Join Date
    Jul 2008
    Posts
    65
    Thanks
    42
    Thanked 0 Times in 0 Posts

    Default SELECT onclick

    Can I have a onclick event on a option tag that changes the background of that option? If so, how do I do it? Thanks

  2. #2
    Join Date
    Aug 2007
    Location
    Ohio
    Posts
    79
    Thanks
    0
    Thanked 15 Times in 15 Posts

    Default

    I believe the CSS background property for option elements isn't supported by all browsers. I remember testing this recently... It works in Firefox 3.0 for sure, but I'm pretty sure the property is ignored in all version of Internet Explorer.

  3. The Following User Says Thank You to jackbenimble4 For This Useful Post:

    hosdank (08-27-2008)

  4. #3
    Join Date
    Jul 2008
    Posts
    128
    Thanks
    0
    Thanked 17 Times in 16 Posts

    Default

    Quote Originally Posted by hosdank View Post
    Can I have a onclick event on a option tag that changes the background of that option? If so, how do I do it? Thanks
    Presumably you mean background colour. You can do something like this:
    Code:
    <select onchange="this.options[this.selectedIndex].style.backgroundColor='#0f0'">
    but results vary.

  5. The Following User Says Thank You to clueful For This Useful Post:

    hosdank (08-27-2008)

  6. #4
    Join Date
    Jul 2008
    Posts
    65
    Thanks
    42
    Thanked 0 Times in 0 Posts

    Default

    Yeah, but the thing is I want all the other backgrounds to change back to what they were. Thanks anyway

  7. #5
    Join Date
    Jul 2008
    Posts
    128
    Thanks
    0
    Thanked 17 Times in 16 Posts

    Default

    Quote Originally Posted by hosdank View Post
    Yeah, but the thing is I want all the other backgrounds to change back to what they were. Thanks anyway
    Yes I thought you might.

    Code:
    <select onchange="setOptionColour(this,'#0f0')">
    ........
    <script type="text/javascript">
        
    function setOptionColour(box, clr)
    {
     for(var i=0, len=box.options.length; i<len; i++)
      box.options[i].style.backgroundColor=(i==box.selectedIndex)?clr:"";
    }
        
    </script>

  8. The Following User Says Thank You to clueful For This Useful Post:

    hosdank (08-27-2008)

  9. #6
    Join Date
    Jul 2008
    Posts
    65
    Thanks
    42
    Thanked 0 Times in 0 Posts

    Default

    Thanks so much

  10. #7
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    But if you check the display of a background-color based options in a select list through Firefox and IE, you'll get two different things

  11. #8
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    You can use the .select() object to select text in a textfield, jsut for later use.

    -magicyte

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •