2

Been tinkering with some code regarding setting the visibility of a drop down based on the selected value of another dropdown.

This code is for the page load:

     $(document).ready(function() {
            if ($("#<%=ddlCoverage.ClientID %>").val()  == 'Basic') {
                $('#CoverageType').show();
            }
            else{
                $('#CoverageType').hide();
            }
        });

Here is the the other piece that I use for the changing of the dropdown.

$("#<%=ddlCoverage.ClientID%>").change(function() {
                $('#CoverageType')[($(this).val() == 'Basic') ? 'show' : 'hide']()
            });

Is there a better way to write this?

1
  • ternary conditional operator can make it in one line though make hard to read Commented Jun 16, 2011 at 16:46

3 Answers 3

9

http://api.jquery.com/toggle/

$('#CoverageType').toggle($("#<%=ddlCoverage.ClientID %>").val()  == 'Basic'); //true or false
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to toggle elements' visibility based on the selection of a drop-down, you'll also need to change the rest of you code a little:

$(function()
{
  $("#<%=ddlCoverage.ClientID %>").change(function()
  {
    $('#CoverageType').toggle($(this).val()  == 'Basic');
  });
});

This way, it'll trigger every time the drop down menu's value is changed as opposed to once when the pages loads. If you only want it when the page loads, why don't you just set it programatically in your ASP/PHP instead of JavaScript?

3 Comments

+1 - Good points, and great code addition based on them. :) Please do see the edit to my answer, however, for how I would write your version.
The dropdown is preseleetced programatically but will only show the second div if the first drop down has 'Basic' selected.
@chopps - If it's set programmatically, why don't you just programmatically hide the DIV instead of using JavaScript?
0

It's not necessarily optimized (although it's no slower either), but this is the way I'd write it:

( function( ddlCoverageClientID )
{
  $( function()
  {
    $( '#CoverageType' ).toggle( $( '#' + ddlCoverageClientID ).val() === 'Basic'  );
  } );
}( '<%=ddlCoverage.ClientID %>' ) );
  1. I prefer to inject dynamic server side langues via IIFE in order to keep my JS more readble (to me anyway)
  2. I prefer the $( function(){} ); shortcut to $( document ).ready( function(){} );
  3. I prefer to toggle with a BOOL rather than check a BOOL and then call show or hide

EDIT:

I would modify Francois Deschenes's excellent code, though. Note that I cache the return of $( '#CoverageType' ) rather than run it every time the select changes.

( function( ddlCoverageClientID )
{
  $( function()
  {
    var $CoverageType = $( '#CoverageType' );

    $( '#' + ddlCoverageClientID ).change( function()
    {
      $CoverageType.toggle( $( this ).val() === 'Basic' );
    } );
  } );  
}( '<%=ddlCoverage.ClientID %>' ) );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.