This is a bit more efficient:

$('.test1').change(function(){
  var test1 = $(this).val();
  var test2 = $(this).closest('li').next('li').find('select.test2').val
();
  alert("Test1: "+test1+"and Test2: "+test2);
});

Or to avoid repeating the traversal every time:

$('#idfortheUL li').each(function(){
  var $t1 = $(this).find('select.test1'),
       $t2 = $(this).find('select.test2');
  $t1.change(function(){
    alert( "test1:" + $t1.val() + "\ntest2:" + $t2.val() );
  });
});

cheers,
- ricardo

On Mar 19, 11:04 am, Jsbeginner <jsbegin...@monarobase.net> wrote:
> 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