Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

set selected item color 2

Status
Not open for further replies.

zfang

Programmer
Apr 3, 2002
23
US
Hi,

Are there any ways to set the selected item color in a drop-down menu to a color other than the default one
(the boring gray)?

Thanks,

George
 
I don't know if this is what u want;
<select name="xyz" style="background-color:Green;">
<option value="1" selected>1option>
<option value="2" >2</option>
</select>

It's always in the details.
 
Thanks for your reply. However, that is not what I want.
I want to set the color of the selected item to a color that's not the default one. Your example is to set the background color of the whole list.
 

Something like this:

Code:
<html>
<head>
<script type="text/javascript">
<!--
	function highlightSelected(selObj, nonSelCol, selCol)
	{
		for (var loop=0; loop<selObj.options.length; loop++)
			selObj.options[loop].style.backgroundColor = (loop != selObj.selectedIndex) ? nonSelCol : selCol;
	}
//-->
</script>
</head>
<body>
	<form>
		<select onchange="highlightSelected(this, '#FFFFFF', '#FF0000');">
			<option value="1">Option 1</option>
			<option value="2">Option 2</option>
			<option value="3">Option 3</option>
		</select>		

		<select onchange="highlightSelected(this, '#FFFFFF', '#00FF00');">
			<option value="1">Option 1</option>
			<option value="2">Option 2</option>
			<option value="3">Option 3</option>
		</select>		
	</form>
</body>
</html>

I've given two different select boxes to show you that it's quite portable.

All you do is place this:

Code:
onchange="highlightSelected(this, '#FFFFFF', '#00FF00');

In your select box, where the second paramater is the "unselected" colour you'd like, and the third is the "selected" colour you'd like... And that's about all there is to it!

Hope this helps,
Dan
 
Hi, Dan:

Thanks for your reply. I tried out your code. The color of the selected item is still the default color. It seems that the background color set in your code is overlayed by the default color, which is kind of gray/blue. Here is what I observed.
The default color is shown on a item,say item 1, I selected. Now, I press
mouse on another item,say item 2, the color of the previously selected item, item 1, changed to the color of the selected item color, which is defined in the code(e.g., red). The color of item 1
was changed back to unselected item color when I released the mouse. But the color of item 2, which is currently selected item , is the boring gray/blue.

George
 

Try modifying the onchange declaration to be:

Code:
onchange="highlightSelected(this, '#FFFFFF', '#FF0000');blur();"

instead.

Hope this helps,
Dan

 
It doesn's work either.
Thanks,

George
 

If you're trying to remove the blue highlight bar that you see as you move the mouse over the dropdown options, then as far as I know, this cannot be done. The solution I've given is the closest you will get to this.

However, I find it strange that you don't see any highlighting at all - both versions work for me in IE6 with no problems. Maybe your browser is screwed?

Dan
 
Not sure if this is what you mean but for the selected option to show the bg color it must be out of focus. So what i did was to move focus to the next element.
Code:
<html>
<head>
<script type="text/javascript">
<!--
    function highlightSelected(selObj, nonSelCol, selCol)
    {
        for (var loop=0; loop<selObj.options.length; loop++)
            selObj.options[loop].style.backgroundColor = (loop != selObj.selectedIndex) ? nonSelCol : selCol;
    }
//-->
</script>
</head>
<body>
    <form>
        <select onchange="highlightSelected(this, '#FFFFFF', '#FF0000');sel.focus();">
            <option value="1">Option 1</option>
            <option value="2">Option 2</option>
            <option value="3">Option 3</option>
        </select>        

        <select id ="sel" onchange="highlightSelected(this, '#FFFFFF', '#00FF00'); hid.focus();">
            <option value="1">Option 1</option>
            <option value="2">Option 2</option>
            <option value="3">Option 3</option>
        </select>
<input type = "submit" value = "submit" id = "hid">        
    </form>
</body>
</html>
Hope that is what you mean.

Glen
 
Glen's version works. Thanks a lot for the help from
you guys.

George
 
Dan I tried your script and the blur() call didn't work for me, so I changed your code to change focus. Let me know if Blur worked for you please. Thanks for the star George!

Glen
 

It did work for me - which is weird... I'm using IE6... How strange!

Guess that goes to show that Javascript that works on one machine might not always work on another!

Dan
 
Oops - how very embarassing!... I just checked my source again, out of interest, only to find that it wasn't the same as the source I posted!... I must have done one of those forgetful coding moments ;o)

If you ignore the "blur();" (not needed ;o)... And add this line to the end of the highlightSelected function, it'll work as I had intended (honest ;o):

Code:
document.getElementsByTagName('body')[0].focus();

Having said all of that, I still stand by my previous comment about Javascript not always working on all identical browsers ;o)

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top