Reputation: 87
I am hiding the default option in the option list when viewing all the options(1,2,3) (when click select 1st time) but then it needs to reappear somehow after making a selection and then viewing the options again..(only by page refresh can user view the select box as containing the category of 'bathroom' otherwise meaningless numbers 1,2,3 appear..)
I.e. Currently 'bathrooms' does not reappear once option is made and then the select box is clicked again..(if user changes mind) this is the issue. What is the best way to do this.
I can't change the html - this needs to be done ONLY with Jquery. Help please
<select name="searchForm__bathrooms" id="field-searchForm__bathrooms" class="field- dropdown">
<option value=""> Bathrooms </option>
<option value="1"> 1 </option>
<option value="3"> 3 </option>
<option value="2"> 2 </option>
</select>
$('select option:first-child').hide();
Upvotes: 0
Views: 1698
Reputation: 5182
http://jsfiddle.net/earlonrails/EfMvd/2/
Javascript
$(function() {
$("#field-searchForm__bathrooms").change(function() {
var selectEle = $(this);
var doAppend = true;
$.each(selectEle.children(), function(index, child) {
if (child.value == "0") {
doAppend = false;
}
});
if (doAppend) {
selectEle.append('<option name="bathroom" value="0">Bathrooms</option>');
}
});
});
HTML
<body>
<select name="searchForm__bathrooms" id="field-searchForm__bathrooms" class="field-dropdown">
<option value="1"> 1 </option>
<option value="2"> 2 </option>
<option value="3"> 3 </option>
</select>
</body>
Upvotes: 1
Reputation: 9818
$("#field-searchForm__bathrooms").click(function(){
$('select option:first-child').toggle();
})
Upvotes: 0