Search code examples
javascriptformsselectselectedindex

Javascript Forms Select Selectedindex


Having problems with the following - Not working as expected, getting JS doc errors as well.

JSFiddle Nota Worka.


Solution

  • try this fiddle: http://jsfiddle.net/maniator/egjF4/6/

    and change the if line to:

    if (document.forms['myform'].selectbox1.selectedIndex == 2)
    

    you need the == to check values

    UPDATE

    Based on your comments below here is the jQuery for the same thing:

    $(function(){
        $('#selectbox1').change(function(){
            if(this.selectedIndex == 2){
                $('#input1, #input2, #asterisk').css('visibility', 'visible');
                $('#input2').addClass('required');
            }
            else {
                $('input, #asterisk').css('visibility', 'hidden');
                $('#input2').removeClass('required');
            }
        })
    })