5

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

Ahmar Ali
  • 1,038
  • 7
  • 27
  • 52
  • 1
    do you have any code ? Provide a clear explanation of what you want to achieve – dawez Nov 06 '14 at 17:29
  • what exactly do you mean by first option? please add a code snippet – Sai Nov 06 '14 at 17:29
  • :first-child pseudo class maybe – Sai Nov 06 '14 at 17:32
  • Not sure about your question but maybe what you are looking for: http://stackoverflow.com/a/8442831/1414562 – A. Wolff Nov 06 '14 at 17:38
  • OK guys, if you're gonna vote to close something, choose a reason that makes sense. The question is fairly clearly stated and is on topic for this site. Code SHOULD DEFINITELY be shown though. – Jasper Nov 06 '14 at 17:38
  • @Jasper IMHO, it is unclear what it is asked because why OP would add an option to then remove it? `drop down which are generated dynamically` – A. Wolff Nov 06 '14 at 17:39
  • @A.Wolff "Hide first option in select" -- really? That reads clearly to me. – Jasper Nov 06 '14 at 17:40
  • @Jasper generated dynamically, ok but HOW then? Why to add it then? I could be completly wrong, maybe question makes sense but not imho. BTW, does this option must or not be default SELECT text or not? – A. Wolff Nov 06 '14 at 17:40
  • @A.Wolff I'd imagine this individual doesn't create the select to begin with. Either way generic advice can be given. Which I think is good anyway, make them think about how to implement. – Jasper Nov 06 '14 at 17:43
  • Sorry if my question doesn't make sense. I have a drop down which i have no control over and i want to hide first option in it. Thanks – Ahmar Ali Nov 06 '14 at 18:25

4 Answers4

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

Community
  • 1
  • 1
Jasper
  • 75,717
  • 14
  • 151
  • 146
9
$('select > option:first').hide();

All your first select option hidden with this.

Gael
  • 167
  • 6
5

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!

Zanoldor
  • 378
  • 6
  • 13
  • I'm sorry, when I was answering and testing the codes and after that I submitted my answer appeared all other answers. I'm sorry. – Zanoldor Nov 07 '14 at 15:37
1

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;
}
Hive7
  • 3,599
  • 2
  • 22
  • 38
  • The selector you provided I believe is incorrectly written. This idea also doesn't appear to work in IE 11. – Jasper Nov 07 '14 at 19:10