Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-20-2012, 03:52 PM   PM User | #1
kevinkhan
Regular Coder

 
Join Date: Jun 2009
Posts: 350
Thanks: 75
Thanked 0 Times in 0 Posts
kevinkhan is an unknown quantity at this point
jquery - change backround color of html select options

I have this html code

PHP Code:
<p> 
                    <label for="contact">Contact name<span class="required"> *</span></label>
                    <select name="contact" id="contact" onClick="clearSelect(this,'contact')">
                    <option value="">Select Contact</option>
                    <?php foreach ($contacts as $contact): ?>
                        <option value="<?php htmlout($contact['id']); ?><?php
                                
if ($contact['id'] == $contactID)
                                    echo 
' selected="selected"';
                                
?>><?php htmlout($contact['name']); ?></option>
                    <?php endforeach; ?>
                    </select>    
                </p>
and this jquery fuction

PHP Code:
function clearSelect(thisfieldid) {

    
$j("#" id).css("background-color","#FFF");
    
$j("#" id).css("border","1px solid #CCCCCC");
    
$j("#" id).css("color","#000000");

When i click the select book the background of the select box changes to white but the drop down list stays the same color untill you scroll down it.

Any ideas how to change all the option background colors to white when the select box is clicked?
kevinkhan is offline   Reply With Quote
Old 11-21-2012, 02:38 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,376
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
Should be working for you.
I suggest you change onClick="clearSelect..... to onchange="clearSelect..... in the select tag.
sunfighter is offline   Reply With Quote
Old 11-21-2012, 04:23 PM   PM User | #3
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,592
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
First of all, this is a pretty – well – “uncommon” way to do it. Secondly, try to style the option elements rather than the select. And try to separate function and style a little more. For example, don’t change CSS with JavaScript, use JS to add a class to the element and style that class with CSS in the stylesheet.

So, for example:
Code:
function clearSelect(thisfield, id) {
    $j("#" + id).addClass('cleared');
}
Code:
.cleared {
  background-color: #FFF;
  border: 1px solid #CCCCCC;
  color: #000;
}
/* to style the option elements you can use .cleared option {…} */
__________________
Don’t click this link!
VIPStephan is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:37 AM.


Advertisement
Log in to turn off these ads.