View Full Version : Grabbing the label of an options optgroup

02-07-2007, 02:28 PM

I'm trying to grab the label of an option's optgroup. Its got to be possible but I just cant find an element for the life of me!

Any help is much appreciated.

02-07-2007, 03:03 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"


<script language="JavaScript" type="text/javascript">
function Tst(){
var obj=document.getElementById('tst1').getElementsByTagName('OPTGROUP')



<body onload="Tst();">
Tested with IE and MozFF
<select id="tst1">
<optgroup label="Swedish Cars">
<option value ="volvo">Volvo</option>
<option value ="saab">Saab</option>
<optgroup label="German Cars">
<option value ="mercedes">Mercedes</option>
<option value ="audi">Audi</option>
<script> vic=0; </script>
<form name=Show id=Show style="position:absolute;visibility:visible;top:360px;left:0px;" >
<textarea name="fred" rows="5" cols="100"></textarea><br>
<input size=100 name=Show0 ><br>
<input size=100 name=Show1 ><br>
<input size=10 name=Show2 >
<input size=10 name=Show3 >
<input size=10 name=Show4 >
<input size=10 name=Show5 >
<input size=10 name=Show6 >
<input size=10 name=Show7 >
<input size=10 name=Show8 >
<input size=10 name=Show9 >


02-08-2007, 12:03 PM
jeez ur from pompey too. thats well a bit too wierd for my liking.

cheers mate.

02-08-2007, 01:31 PM
hang on a bit mate. that doesnt work if the select menu is dynamic, ie from php.

I'd have thought there was a pretty simple way to do it...? I'm gunna keep avin a go, any more help is appreciated. :)

02-08-2007, 01:36 PM
Easy in the end lol...

function selectCategory()
var category = document.getElementById('category');
var parentID = category.options[category.selectedIndex].getAttribute('parent');

var theText = document.getElementById('group_' + parentID).label;

document.getElementById('selectedCategory').innerHTML = theText;

echo "<select name='category' class='form' size='12' id='category' onchange='selectCategory();'>\n";
echo "<optgroup class='letter2' id='group_{$cid}' label='{$_category['category_name']}'>\n";
foreach ($subcats[$cid] as $sid => $sub)
$sel = ($data['category'] == $sid) ? "selected='selected'" : '';
echo "<option parent='{$cid}' class='subcountry' value='{$sid}' {$sel}>&nbsp;&raquo;&nbsp; {$sub['category_name']}</option>\n";
echo "</optgroup>\n";
echo "</select>";

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum