Yaco Zaragoza
Yaco Zaragoza

Reputation: 437

Show & Hide content based on a radio

I have a radio button that hides/shows a content area, but how do I check what value is selected on load?

This is the code that I am using right now

$(document).ready(function(){

    $("#CONTENT_DIV").hide();

    $("input:radio[name='SHOW_HIDE_CONTENT']").change(function(){  
        if(this.value == 'y' && this.checked){
          $("#CONTENT_DIV").show();
        }else{
          $("#CONTENT_DIV").hide();
        }
    });

});

Upvotes: 0

Views: 38

Answers (1)

Milind Anantwar
Milind Anantwar

Reputation: 82251

You can simply trigger the change event after attaching the event:

$("input:radio[name='SHOW_HIDE_CONTENT']").change(function(){  
    if(this.value == 'y' && this.checked){
      $("#CONTENT_DIV").show();
    }else{
      $("#CONTENT_DIV").hide();
    }
}).change(); //trigger change to see changes 

Upvotes: 1

Related Questions