views:

218

answers:

1

Hi All, I have a select list with optgroups. I want to add a handler for the drop down selected index change, how do I tell which optgroup the selected item belongs to? This determines further execution path. I understand how to add the function, the function contents are more the issue.

$ddl.bind("change", function(){
  //how do I find out which option group the selected option belongs to?
  var selectList = $(this); 
});

Thanks for any tips.

Cheers, ~ck

+1  A: 
<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt; 
    <title>test</title>
</head>
<body>
<select id="marcas">
    <optgroup label="ford">
        <option>ka</option>
        <option>fiesta</option>
        <option>mondeo</option>
    </optgroup>
    <optgroup label="peugeot">
        <option>305</option>
        <option>306</option>
        <option>205</option>
    </optgroup>
</select>
</body>
</html>
<script>
$(function(){

    $("#marcas").change(function () {
        alert($(this).find(":selected").parent().attr("label"));
    });

});
</script>
andres descalzo
Thanks for the tip Andres!
Hcabnettek