Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Feb 2007
    Location
    Waterlooville, UK
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Grabbing the label of an options optgroup

    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.

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,524
    Thanks
    3
    Thanked 508 Times in 495 Posts
    Code:
    <!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>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    New to the CF scene
    Join Date
    Feb 2007
    Location
    Waterlooville, UK
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    jeez ur from pompey too. thats well a bit too wierd for my liking.

    cheers mate.

  • #4
    New to the CF scene
    Join Date
    Feb 2007
    Location
    Waterlooville, UK
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    New to the CF scene
    Join Date
    Feb 2007
    Location
    Waterlooville, UK
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Easy in the end lol...

    Code:
    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;	
    }
    Code:
    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>";
    Last edited by matt7405; 02-08-2007 at 01:38 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •