Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide optgroup/option elements?

Is there a way to hide option or optgroup HTML elements? I've tried calling hide() in jQuery, and also using regular Javascript to set style.display='none'.

It works in Firefox but not in any other browsers. Actually removing them from the DOM does work, so perhaps there's a way to save each DOM element when it's removed, and reinsert them in the same place?

My HTML is like this:

<select name="propsearch[area]" id="propsearch_area">     <option value="0">- Any -</option>     <optgroup label="Bristol">         <option  value="Hotwells">Hotwells</option>         <option  value="Montpelier">Montpelier</option>     </optgroup>     <optgroup label="Cardiff">         <option  value="Heath">Heath</option>         <option  value="Roath">Roath</option>     </optgroup>     <optgroup label="Exeter">         <option  value="Pennsylvania Road">Pennsylvania Road</option>         <option  value="Lower North Street">Lower North Street</option>     </optgroup>     <optgroup label="Swansea">         <option  value="Brynmill">Brynmill</option>         <option  value="Uplands">Uplands</option>     </optgroup> </select> 
like image 852
DisgruntledGoat Avatar asked Apr 28 '10 17:04

DisgruntledGoat


People also ask

What does Optgroup element do?

The <optgroup> tag is used to group related options in a <select> element (drop-down list). If you have a long list of options, groups of related options are easier to handle for a user.

How do you style Optgroup labels?

optgroup { font-style: normal|italic|oblique; } styles the optgroup label and options within the optgroup. option { font-style: normal|italic|oblique; } has no effect. select { font-weight: normal; } has no effect but select { font-weight: bold; } will make all options in the select box bold.

What is the specific purpose of the Optgroup HTML tag in creating a drop-down list?

The optgroup element is used to group options in a drop-down list, with the aim of making the list easier to navigate. The functionality essentially splits large numbers of <option> items into small sections. Note that the label attribute is required, and will be rendered as the first item in the section.


1 Answers

I figured that this solution works fine for me:

Make another select e.g.

$("#footer_canvas").after('<select id="parkingLot"></select>'); 

then hide it

$("#parkingLot").hide(); 

When you want to 'hide' some optgroup, just 'park' it in this hidden select.

$('#VehicleVehicleCategoryId optgroup[label="kategorie L"]').appendTo("#parkingLot"); 

Same way you can make it visible. This is just the snippets of my solution, that works fine for me.

like image 116
Vlado Tovarnak Avatar answered Sep 23 '22 16:09

Vlado Tovarnak