Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript solution for hiding dropdown list options

Edit: Thanks everybody, but nothing seems to work. I am inserting this code in a file that I know is being used and that contains other javascript blocks normally formatted, and this still doesn't work. It works in a fiddle, but not on my code. I guess this is too specific to the platform and extension that I'm trying to modify (this is part of a Magento checkout step modified by a third party extension). I will start looking into replacing the list with a manually generated one. Thanks again.


I am trying to hide an option in a dropdown list that is dinamically generated. The CSS solution doesn't work on all browsers, and even though I have found several similar questions here, neither one offers a solution that works for me.

Here's what my list renders like:

<select id="timeselect" name="adj[delivery_time][]" title="El plazo de la entrega" class="adjtimeselect select" type="time" ><option id="option-10" value="10" >10</option>
<option id="option-11" value="11" >11</option>
<option id="option-12" value="12" >12</option>
<option id="option-13" value="13" >13</option>
<option id="option-14" value="14" >14</option>
<option id="option-15" value="15" >15</option>
<option id="option-16" value="16" >16</option>
<option id="option-17" value="17" >17</option>
<option id="option-18" value="18" >18</option>
<option id="option-19" value="19" >19</option>
<option id="option-20" value="20" >20</option>
</select> 

I need to hide the option with value "12" for example. I am using this JS:

$("#timeselect option[value='12']").remove();

Any advice would be greatly appreciated since I'm new to JS.

Thanks.

like image 698
Palmer Del Campo Avatar asked Aug 18 '13 15:08

Palmer Del Campo


People also ask

How do you hide items in dropdown?

You can hide the dropdown arrow from the DropDownButton by adding class e-caret-hide to DropDownButton element using cssClass property.

How do I hide a drop-down list in HTML?

We use <select> and <option> elements to create a drop-down list and use disabled attribute in <select> element to disable the drop-down list element.

How do you hide a drop-down list in CSS?

Answer: Use the CSS :hover pseudo-class If you simply want to show and hide dropdown menu on mouse hover you don't need any JavaScript. You can do this simply using the CSS display property and :hover pseudo-class.

How do you hide a field in JavaScript?

Style display property is used to hide and show the content of HTML DOM by accessing the DOM element using JavaScript/jQuery. To hide an element, set the style display property to “none”. document. getElementById("element").


1 Answers

Jquery remove by value

$("#timeselect option[value=11]").remove();

Jquery remove by Text

$("#timeselect option:contains(11)").remove();

Jquery to hide a select box option with its value using css

   $("#timeselect option[value='11']").hide();

or

   $("#timeselect option[value='11']").css('display','none');
like image 167
Amith Avatar answered Sep 21 '22 17:09

Amith