Hello,

I'm trying to get the value of an option in a select list contained in a different li item...

Here is the html code :

<ul>
<li><p><select class="test1">
<option value="1" selected="selected">Option 1</option>
<option value="2">Option 2</option>
</select></p></li>
<li><p><select class="test2">
<option value="3">Option 3</option>
<option value="4"  selected="selected">Option 4</option>
</select></p></li>
</ul>

<ul>
<li><p><select class="test1">
<option value="1" selected="selected">Option 1</option>
<option value="2">Option 2</option>
</select></p></li>
<li><p><select class="test2">
<option value="3" selected="selected">Option 3</option>
<option value="4" >Option 4</option>
</select></p></li>
</ul>

I would like to be able to get the text of .test2 option:selected when I change the value of .test1 option selected ...

Here is the JS code I've written .:

$(".test1").change(function(){
var test1 = $("option:selected", this).val();
var test2 = $(this).closest("ul").find("li > p > .test2 option:selected").val();
alert("Test1 : "+test1+" and Test2 :"+test2);
});

Is this the best way? or is there a better way to achieve the same result ?


Thankyou.


Reply via email to