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
The hidden attribute hides the <select> element. You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid. A hidden <select> element is not visible, but it maintains its position on the page.
The below codes works fine for two dropdowns, but not for more than 2. var $select = $("select[id$='go']"); $select. change(function() { $select . not(this) .
$("#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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With