473,386 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

hide and/or sort OPTGROUP using javascript

I think i am quite experienced javascript programmer, but I got a
problem.
I have a selectbox with e.g. 17 optgroups with 100 options.
I need a javascript code to hide some of that optgroups (i can give
each optgroup individual ID, e.g. id="group1"..."group"17).
e.g. the html code looks like:
<select name="mySelect" id="mySelect">
<optgroup label="First group" id="group1" >
<option value="a" >
Item A
</option>
<option value="b" >
Item B
</option>
</optgroup>
<optgroup label="Second group" id="group2" >
<option value="a" >
Item A
</option>
<option value="b" >
Item B
</option>
</optgroup>
</select>

The problem is, that no code i was able in last 3 hours invent works. I
tried e.g.
document.getElementById("group1").className="hidde n"; //style .hidden
{display:none}
document.getElementById("group1").style.display=no ne;
and many other variants. I can easily hide this way the whole select,
in FF i can hide also option, but i can never hide the whole group.

If this is a "feature" and there is no sollution, you may help me to
find something different - i need javascript code to move one whole
optgroup to the beggining of the list. Or (this may be the sollution
for the first problem): get one group and make a new selectbox just
after the first one (and then i would hide the first).

P.S.: i do not want ajax lists, just some easy client side sollution

Aug 17 '06 #1
1 4579
TKapler wrote:
I think i am quite experienced javascript programmer, but I got a
problem.
I have a selectbox with e.g. 17 optgroups with 100 options.
I need a javascript code to hide some of that optgroups (i can give
each optgroup individual ID, e.g. id="group1"..."group"17).
e.g. the html code looks like:
<select name="mySelect" id="mySelect">
<optgroup label="First group" id="group1" >
<option value="a" >
Item A
</option>
<option value="b" >
Item B
</option>
</optgroup>
<optgroup label="Second group" id="group2" >
<option value="a" >
Item A
</option>
<option value="b" >
Item B
</option>
</optgroup>
</select>

The problem is, that no code i was able in last 3 hours invent works. I
tried e.g.
document.getElementById("group1").className="hidde n"; //style .hidden
{display:none}
document.getElementById("group1").style.display=no ne;
and many other variants. I can easily hide this way the whole select,
in FF i can hide also option, but i can never hide the whole group.

If this is a "feature" and there is no sollution, you may help me to
find something different - i need javascript code to move one whole
optgroup to the beggining of the list. Or (this may be the sollution
for the first problem): get one group and make a new selectbox just
after the first one (and then i would hide the first).

P.S.: i do not want ajax lists, just some easy client side sollution
Hello TKapler,

here is a quick & dirty solution. Ive tried your display thing, but i
didnt work on my, either. Next step was some funny {position=
'absolute'; visibility= 'hidden'}, but no luck in IEX so far (FF did it
;)).
so, the only thing remains (as far as i know) would be dom-removal. i
dont know your your envirmonent, but it should work on you
(select-status stays fine).

So, here it is, tested on IEX v6.0.2 and FF v1.5.0.5 :

// -- after removal, 'groupX' cant be accesed via getElementById .. so
we store it globally
var hNodeCache= {};

function toggle(sNodeId) {
// -- first call, store node in cache for later usage
if (!hNodeCache[sNodeId]) {
hNodeCache[sNodeId]= document.getElementById(sNodeId);
hNodeCache[sNodeId]._nextNode= hNodeCache[sNodeId].nextSibling;
}
var n= hNodeCache[sNodeId], m= document.getElementById('mySelect');

if (n.parentNode && n.parentNode.nodeType== 1) // -- wipe it! nodeType
check for iex, cause he thinks #document-fragment or some is a "real"
(parent)node, how silly
n.parentNode.removeChild(n);
else {
var nn= n._nextNode; // -- we cant insertBefore a child, which is
currently not there, get the next!
while (nn && (!nn.parentNode || nn.parentNode.nodeType!= 1)) nn=
nn._nextNode;

if (nn) m.insertBefore(n, nn); // -- somewhere, but not last
else m.appendChild(n); // -- is last
}
}
The call would be:
<a href="javascript:toggle('group1')">toggle optgroup 1</a>

In the example above, ive inited the hNodeCache elements on-the-fly,
when needed. Probably, its a lot better to pre-init them, maybe onload
or some.. In this case, you could also get rid of these hundreds ids,
but as mentioned, i dont know your envirmonent...

good luck
Ulrich

Aug 19 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: cedawe | last post by:
I have two select boxes. When the user picks a value in the first one it completely re-populates the second one. It works fine, but only generates a standard OPTIONS list and I now want to group...
10
by: Ryan McGeary | last post by:
In a <select> drop-down, the onchange event isn't called when scrolling through the dropdown using the mouse-wheel and when crossing over a new <optgroup>. Using the example below, notice how...
3
by: veganeater | last post by:
Hi, I'm wondering if there's a way that I can select which <div> to show based on the user's selection from a dropdown/listbox form. <form name="form1" method="post" action=""> <select...
3
by: Stewart | last post by:
Dear comp.lang.javascript, I have more than once wanted to manipulate the contents of select boxes dynamically, whilst the boxes contain <optgroup> tags. Manipulation of a select box containing...
2
by: | last post by:
I have a page where I have 3 combo boxes listed in a column. sort of like: combo1 combo2 combo3 Based on which one is clicked, the others are supposed to hide (i.e. combobox.visible =...
1
by: millw0rm | last post by:
hi, how to create a optgroup dynamically based on the value of an array... for eg., $data =...
5
by: srampally | last post by:
I need the capabilty to hide/show a selection list, just the way its done at http://www.lufthansa.com (place the cursor over "Group Companies"). However, I am looking for a javascript that is much...
6
by: Norman | last post by:
Hello, I have a working Show / Hide form, that works on FF, but what I would like to do is to be able to display one part when a user clicks on one radio button and display another part when the...
4
by: Patrick Nolan | last post by:
I am using javascript to manipulate optgroups in select elements. When the results are displayed in Firefox 2.0 there is an annoying blank line at the top of the multi-line select box. This...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.