3
\$\begingroup\$

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 () {
    $('.Form').hide();
    $('input').click(function () {
        <!--check if its One-->
        if ($('input[value=informed]:checked').length) {
            $('#ContactFormOne').show();
            $("#ContactFormTwo, #ContactFormThree, #ContactFormFour, #ContactFormFive, #ContactSix'").hide();
            <!--check if its Two-->
        } else if ($('input[value=release]:checked').length) {
            $('#ContactFormTwo').show();
            $("#ContactFormOne, #ContactFormThree, #ContactFormFour, #ContactFormFive, #ContactSix'").hide();
            <!--check if its Three-->
        } else if ($('input[value=intake]:checked').length) {
            $('#ContactFormThree').show();
            $("#ContactFormTwo, #ContactFormOne, #ContactFormFour, #ContactFormFive, #ContactSix'").hide();
            <!--check if its Four-->      
        } else if ($('input[value=checklist]:checked').length) {
            $('#ContactFormFour').show();
            $("#ContactFormTwo, #ContactFormOne, #ContactFormThree, #ContactFormFive, #ContactSix'").hide();
            <!--check if its Fiver-->     
        } else if ($('input[value=health]:checked').length) {
            $('#ContactFormFive').show();
            $("#ContactFormTwo, #ContactFormOne, #ContactFormThree, #ContactFormFour, #ContactSix'").hide();
        }
    })
});
\$\endgroup\$
  • \$\begingroup\$ Why not use the value passed as an id for the form elements? \$\endgroup\$ – hjpotter92 Mar 11 '13 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 '13 at 8:29
3
\$\begingroup\$

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 () {
    $('.Form').hide();
    $('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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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