BernardA
BernardA

Reputation: 1523

how to return SELECT to default CSS after changing with JQUERY on SELECTED

I want to highlight the user's choices on a series of SELECT, which I have accomplished with JQUERY.

Problem is that if the SELECT is returned to the default position it does not change back to 'plain' SELECT ( without the highlight).

Additionally the change with JQUERY changes the format of the SELECT BOX, somehow.

Please see fiddle at http://jsfiddle.net/BernardA/bM3p4/

My internet connection is limited these days, so I may be slow to respond to queries.

Thanks for your help!

The HTML is as below:

<form id="filtermodel">

<select id="fuelselect" class="specselect" name="fuel" >
<option value="none"> Fuel </option>
<option value="gas">Essence </option>
<option value="diesel">Diesel</option>
</select>

<select id="powerselect" class="specselect" name="power" >
<option value="0"> cv  </option>
<option value="100"> >100 hp </option>
<option value="150"> >150 hp </option>
<option value="200"> >200 hp </option>
</select>

<select id="mileageselect" class="specselect" name="mileage" >
<option value="100"> Mileage</option>
<option value="5"> <5l/100km </option>
<option value="6"> <6l/100km </option>
<option value="7"> <7l/100km </option>
</select>

<select id="co2select" class="specselect" name="co2" >
<option value="1000">co2</option>
<option value="100"> <100 co2 </option>
<option value="150"> <150 co2 </option>
<option value="180"> <180 co2</option>
</select>

<select id="doorsselect" class="specselect" name="doors" >
<option value="none">Doors</option>
<option value="two"> 2 </option>
<option value="three"> 3 </option>
<option value="four"> 4 </option>
<option value="five"> 5 </option>
</select>

<select id="trunkselect" class="specselect" name="trunk" >
<option value="0">trunk </option>
<option value="300"> >300 l </option>
<option value="500"> >500 l </option>
<option value="700"> >700 l </option>
</select>

And the JQUERY:

   $(function(){
 $("#filtermodel select").change(function (){
if("select option:selected"){
$(this).css("background", "#e90707");
$(this).css("color","#ffffff");
}
else{
    $(this).css("background", "");
$(this).css("color","#696969"); 
}
});
});

Upvotes: 0

Views: 238

Answers (2)

DFord
DFord

Reputation: 2358

Even when you selected you default value, you were still changing the background color because the Fuel was being selected. I fixed the jsfiddle here: http://jsfiddle.net/bM3p4/2/

$(function(){
    $("#filtermodel select").change(function (){
        if($("#filtermodel option:selected").val() === "none"){
            $(this).css("background", "");
            $(this).css("color","#696969"); 
        } else if("select option:selected"){
            $(this).css("background", "#e90707");
            $(this).css("color","#ffffff");
        }
    });
})

;

Upvotes: 0

Anupam
Anupam

Reputation: 8016

DEMO HERE

$("#filtermodel select").change(function (){
    var selected = $(this).find("option:selected");
    var first = $(this).find("option:first");
    if(selected.val()!=first.val()){
        $(this).css("background", "#e90707");
        $(this).css("color","#ffffff");
    }
    else{
        $(this).css("background", "");
        $(this).css("color","#696969"); 
    }
});

Upvotes: 2

Related Questions