This piece of code I have written works, but I don't think its the best solution.

What I am doing is check if a certain radio is checked if it is show this div if else show this div if this radio is checked etc.

Can someone point me in the right direction on this one? I would love to know how to write less do more.

$(document).ready(function () {
    $('input').click(function () {
        <!--check if its One-->
        if ($('input[value=informed]:checked').length) {
            $("#ContactFormTwo, #ContactFormThree, #ContactFormFour, #ContactFormFive, #ContactSix'").hide();
            <!--check if its Two-->
        } else if ($('input[value=release]:checked').length) {
            $("#ContactFormOne, #ContactFormThree, #ContactFormFour, #ContactFormFive, #ContactSix'").hide();
            <!--check if its Three-->
        } else if ($('input[value=intake]:checked').length) {
            $("#ContactFormTwo, #ContactFormOne, #ContactFormFour, #ContactFormFive, #ContactSix'").hide();
            <!--check if its Four-->      
        } else if ($('input[value=checklist]:checked').length) {
            $("#ContactFormTwo, #ContactFormOne, #ContactFormThree, #ContactFormFive, #ContactSix'").hide();
            <!--check if its Fiver-->     
        } else if ($('input[value=health]:checked').length) {
            $("#ContactFormTwo, #ContactFormOne, #ContactFormThree, #ContactFormFour, #ContactSix'").hide();
  • \$\begingroup\$ Why not use the value passed as an id for the form elements? \$\endgroup\$
    – hjpotter92
    Mar 11, 2013 at 8:18
  • \$\begingroup\$ How could i use the selector (this) to set up the code above to only use one if statement. So the if statement knows which input value was clicked and shows div with the relative hook and hide all the other divs. \$\endgroup\$
    – user23012
    Mar 11, 2013 at 8:29

1 Answer 1


Though your conditions are fine, I'd suggest you for a radical change in the code.

  1. Change the ids as follows:
    • ContactFormOne to informed
    • ContactFormTwo to release
    • ContactFormThree to intake
    • ContactFormFour to checklist
    • ContactFormFive to health
  2. The code will get a lot shorter now.

The code will be:

$(document).ready(function () {
    $('input').on( 'change', function () {
        <!--check if its One-->
        var ValUe = $(this).val();
        $('.Form').hide(); //Replace the '.Form' selector with the element that is containing your contact forms.
        $('#' + ValUe).show();

where, I'm assuming you have jQuery 1.9.1.

For a better answer, please include your HTML in the question too.


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.