I have options in a drop down which are generated dynamically. I want to hide first option as that is not needed. Is there any cross browser compatible method to achieve this. Any solution would be good whether using jquery or css.
Ahmar
I have options in a drop down which are generated dynamically. I want to hide first option as that is not needed. Is there any cross browser compatible method to achieve this. Any solution would be good whether using jquery or css.
Ahmar
$("#my-drop-down-select-element-id").find("option").eq(0).remove();
You can remove the first <option />
element in a <select />
element with the above code.
Using CSS to hide <option />
elements is problematic. See the link below for more on this.
To break-down the above function chain:
<select />
element. (No pun intended)<option />
elements within the previously selected element. I used find()
because if there are option groups then children()
won't work.<option />
element.This has definitely been asked before, for some more reading: How to hide a <option> in a <select> menu with CSS?
$('select > option:first').hide();
All your first select option hidden with this.
HTML
<select id='test'>
<option value='1'> op 1 </option>
<option value='2'> op 2 </option>
<option value='3'> op 3 </option>
<option value='4'> op 4 </option>
</select>
Jquery
$(document).ready(function() {
$("#test").find("option").eq(0).remove();
});
that's it!
What you can do is use CSS like this as it works in sync with the select loading like this:
#select-item:first-child {
display: none;
}