Need some help with jQuery...

jQuery seems like it would be able to handle something like the example below fairly easily:

I have a form that includes several data fields, one of them being a select box. One of the choices in that select box is ‘other.’

When ‘other’ is selected, I’d like jQuery to display another text field for more specification of whatever ‘other’ might be. The field would be hidden unless ‘other’ is selected. My main script is written in Perl with some HTML.

Thanks for looking.

You can use jQuery to install a click event handler on the checkbox, and show/hide the form element accordingly. Let’s say you have a form that looks like this:



<form id="myform">
<input type="checkbox" id="mycb" name="other_cb" />
<input type="text" id="mytext" name="other_text" style="display: none;" />
</form>


Then you could put something like this in your window.onload handler:



$( '#mycb' ).click( function() { 
    if ( $( '#mycb' ).is( ':checked' ) ) { 
        return $( '#mytext' ).show();
    }

    $( '#mytext' ).hide();
} );


The “return” isn’t necessary – it just lets us break out of the function instead of doing an ‘else’ block.