Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide first option in select [duplicate]

Tags:

html

jquery

css

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

like image 850
Ahmar Ali Avatar asked Nov 06 '14 17:11

Ahmar Ali


People also ask

How do you hide a selected element?

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.

How can we prevent duplicate values in multiple dropdowns in jquery?

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) .


1 Answers

$("#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:

  1. Select the <select /> element. (No pun intended)
  2. Find <option /> elements within the previously selected element. I used find() because if there are option groups then children() won't work.
  3. Filter down to only the first <option /> element.
  4. Remove that element from the DOM.

This has definitely been asked before, for some more reading: How to hide a <option> in a <select> menu with CSS?

like image 173
Jasper Avatar answered Oct 09 '22 00:10

Jasper