Jump to content
zubair439

How to remove the background color in select option tag with reset button?

Recommended Posts

When I am clicking reset, the background color in select field is not changing to white. Its displaying the color of the last selected option. Please help me out with this problem. I want to remove the background color too when I am clicking reset button. Thanks in advance for your help..

 

<body><form><input type="reset" class="resetButton" /><select id="select" style="color: white; width:60px;"onchange="changecolor(select,value)"><option value="white" style="background-color: white; "></option><option value="red" style="background-color: red; color: white;">AB</option><option value="blue" style="background-color: blue; color: white;">BC</option><option value="green" style="background-color: green; color: white;">CD</option><option value="orange" style="background-color: orange; color: white;">DE</option></select></form></body><script>$(function(){$('.resetButton').click(function(){$(this).closest('form').find('select').each(function(){$(this)[0].selectedIndex=0;});});});</script><script language="javascript" type="text/javascript">function changecolor(id,color){id.style.backgroundColor=color;}</script>

Share this post


Link to post
Share on other sites

using javascript to change the selectedIndex of select element does not cause it to fire the onchange event. you need to manually change the color.

<body>  <form>    <input type="reset" class="resetButton" />    <select id="colors">      <option value="white"></option>      <option value="red">AB</option>      <option value="blue">BC</option>      <option value="green">CD</option>      <option value="orange">DE</option>    </select>  </form>  <script>  $('document').ready(function(){    $("#colors").change(function(){      $(this).css({"background-color":$(this).val()});    });    $('.resetButton').click(function(){      $("#colors").val("white").change();    });  });  </script></body>
here I wrote it sticking closer to jquery own functions Edited by Hadien

Share this post


Link to post
Share on other sites

Style options

 

css (does not require JavaScript to be enabled)

            #colors option {color: white;}            option[value='red']{background-color: red;}            option[value='blue']{background-color: blue;}            option[value='green']{background-color: green;}            option[value='orange']{background-color: orange;}

or

 

JQuery

$("#colors option").each(function() {                 $(this).css({'background-color': $(this).val(), 'color': 'white'});                 });

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...