...

View Full Version : Grabbing the label of an options optgroup



matt7405
02-07-2007, 03:28 PM
Hello!

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.

vwphillips
02-07-2007, 04:03 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
<script language="JavaScript" type="text/javascript">
<!--
function Tst(){
var obj=document.getElementById('tst1').getElementsByTagName('OPTGROUP')
document.Show.Show0.value=obj.length
document.Show.Show1.value=obj[0].label
document.Show.Show2.value=obj[1].label
document.Show.Show3.value=obj[0].getAttribute('label');
document.Show.Show4.value=obj[1].getAttribute('label');

}

//-->
</script></head>

<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>
<optgroup label="German Cars">
<option value ="mercedes">Mercedes</option>
<option value ="audi">Audi</option>
</optgroup>
</select>
<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 >
</form>
</body>

</html>

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

cheers mate.

matt7405
02-08-2007, 02: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. :)

matt7405
02-08-2007, 02: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