Re: Disallowing selectbox change

Santiago Bustelo <[email protected]> Thu, 20 Apr 2006 21:17:45 -0300
Newsgroups gmane.comp.web.dom.wdf
Message-ID <[email protected]>
Hi David,

The OPTGROUP tag is an standard, cross-browser way to show non- 
selectable "titles" on a SELECT, that seems to fit your needs.

Moreover, OPTGROUPs are not reflected in the SELECT's.options array,  
nor they increment the selectedIndex value.

In fact, for JavaScript's older form.select methods, they simply  
don't exist. So if you have to create a SELECT with OPTGROUPs  
dynamically, you'll have to resort to DOM manipulation (instead of  
simply adding elements to select.options array).


Sample code follows. The onchange's alert is meant to show how the  
OPTGROUPs are ignored:

<select onchange="alert(this.selectedIndex+'/'+this.options.length);">
	<optgroup label="Category A">
		<option> a1 </option>
		<option> a2 </option>
	</optgroup>
	<optgroup label="Category B">
		<option> b1 </option>
		<option> b2 </option>
	</optgroup>
</select>


--
Santiago Bustelo // icograma
Design & Development
Buenos Aires, Argentina



On 20/04/2006, at 15:23, Gale, David wrote:


> I'm working on a fairly standard drop-down select navigation  
> widget; now
> my user wants to add categories to the select, thusly:
>
> -- Category1 --
> Page1
> Page2
> Page3
> -- Category2 --
> Page4
> Page5
> Page6
>
> When the page is loaded, the appropriate item is selected.  If a
> different "PageN" option is selected, the onchange fires, redirecting
> appropriately; if one of the Category option is picked, it doesn't
> submit (easily done).  However, if a Category option is selected, it's
> possible to then select the original page link, causing the page to
> reload.
>
> Is there any way in the onchange handler to disallow the change-- 
> so, if
> the selected option has no value, the select box returns to the
> previously selected option?  Firefox allows me to disable the Category
> options entirely, which is nice (and avoids the issue), but IE  
> doesn't.
> I thought returning false might do the trick, as that works for form
> onsubmits, but no dice.  The only other option I can think of is to
> record where the select ought to be, and, if the user tries to pick
> something invalid, force the selectedIndex back to the original, but
> this feels somewhat kludgy.  Is there a better option I'm not aware  
> of?
>
> Thanks,
> -David
>


Unsubscribe
[email protected]

List info
http://www.quirksmode.org/dom/list.html 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/wdf-dom/

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/