HTML Tutorial


 Forum HomeForum Home   FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
RegisterRegister - Not registered yet? Got something to say? Join HTML Code Tutorial!
changing <select> background color
Post new topic   Reply to topic    HTML Help Forum Index -> DHTML
View previous topic :: View next topic  
Author Message
lyakasal



Joined: 22 Jan 2005
Posts: 2

PostPosted: Sat Jan 22, 2005 11:23 pm     changing <select> background color Reply with quote

hello,
I am trying to change the background color of a select listbox,
then display some message and then change it again:

var prev_color = select.style.backgroundColor;
select.style.backgroundColor = 'red';
alert('Message');
select.style.backgroundColor = prev_color;

But when I have only one item in my listbox, this does not seem to work...
kanenas



Joined: 14 Dec 2004
Posts: 191

PostPosted: Tue Jan 25, 2005 1:04 am     Code fragment worked Reply with quote

The code fragment you gave worked fine for me. Is it only called from an onchange event handler?
lyakasal



Joined: 22 Jan 2005
Posts: 2

PostPosted: Tue Jan 25, 2005 3:35 am     Reply with quote

OK, the problem was that the size of the listbox was 6,
but it held just one <option>.
So when changing the background color, the "empty spaces" were
not affected (for some reason:)).

I solved it by temporarily adding empty Options and removing them
after the alert message.

Here is the code:
--------------------------------------------------------------------------
//Add some empty options, to match the size of the select box,
// otherwise the style change behaves weird
var options_to_add = select_elem.size - select_elem.length;
var added_options = 0;
for (added_options = 0; added_options < options_to_add;
++added_options)
select_elem.add(new Option(" "));

//OK, now change the style
var prev_class = select_elem.className;
select_elem.className = "select_default_error"; //
select_elem.focus();

alert("Message");

//change the style back and remove the added extra options
select_elem.className = prev_class;
for ( ; options_to_add > 0; --options_to_add)
select_elem.options[select_elem.length - 1] = null;
--------------------------------
Clever, no?
Tell me what you think.
kanenas



Joined: 14 Dec 2004
Posts: 191

PostPosted: Fri Jan 28, 2005 2:01 am     IE bug Reply with quote

I tried the example again in Mozilla after setting the 'size' attribute and it still worked. That's when I figured it was an IE bug. If the number of options in a select box is less than the size and the background is changed, IE repaints only the portion of the select covered by options (try having 3 options with the select box size set to 6). Occluding then uncovering the select box will cause the background to be drawn in the area not containing options. This is definitely a bug; the repaint should be the same, whether IE or Windows initiates it.
Display posts from previous:   
Post new topic   Reply to topic    HTML Help Forum Index -> DHTML All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
HTML Help Archive
Powered by phpBB © 2001, 2005 phpBB Group
HTML Help topic RSS feed 

 
HOSTING / DESIGN
MAKE MONEY

Home
  |   Tutorials   |   Forum   |   Quick List   |   Link Directory   |   About
Copyright ©1997-2002 Idocs and ©2002-2007 HTML Code Tutorial