11

I've got a select list like this:

<select id="selectlist" name="selectproduct" >
    <option value=""> --- Select product  --- </option>
    <option value="1">Product 1</option>
    <option value="2">Product 2</option>
    <option value="3">Product 3</option>
    <option value="4">Product 4</option>
</select>

Unfortunately I can't edit it. Is there any method which let me hide the "Product 4" option by default? I'm trying with CSS, but it doesn't work with IE.

user2132234
  • 301
  • 2
  • 4
  • 14

11 Answers11

10

To hide it, wrap it with a span tag.

$( "#selectlist option[value=4]" ).wrap( "<span>" );

To show it, unwrap the span tag if it has one.

if ( $( "#selectlist option[value=4]" ).parent().is( "span" ) ){
    $( "#selectlist option[value=4]" ).unwrap();
}
Blazemonger
  • 90,923
  • 26
  • 142
  • 180
walterquez
  • 349
  • 2
  • 11
  • 3
    That would create illegal HTML – mplungjan Apr 01 '14 at 04:35
  • I don't think "illegal" and "solution" have any relation or the world might look a lot different. But this might be an easier way to unwrap: $( "#selectlist span > option[value=4]" ).unwrap(); – user1566694 Oct 14 '15 at 17:58
5

using css to hide options is not supported in IE, so you need to update the options list itself.

Try something like

$('#selectlist option[value=4]').remove();

Demo: Fiddle

or if you want to enable it later

var sel = $('#selectlist');
var opts = sel.find('option');

$(':checkbox').click(function(){
    sel.empty().append(this.checked ? opts : opts.filter('[value!=4]'));
}).click()

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
5

You can hide option using following line include in scripting.

    $("#selectlist option[value='4']").hide();

And if you want to again show this option use following line.

   $("#selectlist option[value='4']").show();
2

To save your time, please read the following answer.

OK, I searched for a few hours and I noticed that there is NO WORKING WAY to "hide" (what I mean is make the option temporally disappeared from the drop-down list) for all browsers, such as set the visibility to be hidden or style="display: none".

My final approach is: 1. Create an array to store all the options for the dropdown list. 2. Use a method to dynamically update the drop-down list according what you want to be shown.

That's it, if you want to use a drop down list without using any library

johnnyman
  • 129
  • 2
  • 11
1

You can "hide" the option by moving it to a hidden select element or cached document fragment, then move it back when you want to show it:

var selectTool = (function() {
  var frag = document.createDocumentFragment();
  return {
    hideOpt: function (selectId, optIndex) {
      var sel = document.getElementById(selectId);
      var opt = sel && sel[optIndex];
      console.log(opt);

      if (opt) {
        frag.appendChild(opt);
      }
    },

    showOpt: function (selectId) {
      var sel = document.getElementById(selectId);
      var opt = frag.firstChild;
      if (sel && opt) {
        sel.appendChild(opt);
      }
    }
  }
}());

Then you can hide the 4th option like:

<input type="button" value="Hide option" onclick="
  selectTool.hideOpt('selectlist',4);
">

and show it again using:

<input type="button" value="Show option" onclick="
  selectTool.showOpt('selectlist');
">

All play code of course, but you should get some ideas. If you want to store many options, you'll need a way of referencing them so maybe store them in an object with some form of referencing scheme.

RobG
  • 142,382
  • 31
  • 172
  • 209
0

Try to disable that option with disabled attribute.

<select id="selectlist" name="selectproduct" >
 <option value=""> --- Select product  --- </option>
 <option value="1">Product 1</option>
 <option value="2">Product 2</option>
 <option value="3">Product 3</option>
 <option value="4" disabled="disabled">Product 4</option>
</select>

Check demo

Chirag Vidani
  • 2,519
  • 18
  • 26
  • 1
    Disabling an option doesn't hide it, it just makes it not selectable. And the disabled attribute doesn't have a value. – RobG Jul 15 '13 at 11:14
0

see this link

http://jsfiddle.net/DKKMN/

<select id="selectlist" name="selectproduct" >
 <option value=""> --- Select product  --- </option>
 <option value="1">Product 1</option>
 <option value="2">Product 2</option>
 <option value="3">Product 3</option>
 <option value="4" id="i">Product 4</option>
</select>


#i{display:none;}

create an id and in style make it invisble

0

I suppose you are using JQuery as well.

$("#selectlist option[value='option4']").remove();
sla55er
  • 791
  • 1
  • 8
  • 16
0

to append the child below the list of option using java script.

`var option = document.createElement("option");
 option.text = "All Users";
 option.value = "all_user";
 var select = document.getElementById("log_user_type");
 select.appendChild(option);`
0

if you want to remove some option from select list you can use this code:

<script type="text/javascript">
    $(window).bind("load", function() {    
        $('#select_list_id option[value="AB"],option[value="SK"]').remove();
    });
</script>
Sandeep Sherpur
  • 2,418
  • 25
  • 27
-3
<option value="4" style="display:none">Product 4</option> 

hides the 4th option by default.

kayess
  • 3,384
  • 9
  • 28
  • 45