Although there are ways to simplify your JS code, I will keep the general format and change it a bit for the effect. Note that you can use a comma to select multiple div objects.
You can make the code clean by adding animations like rkamath. It offers a slide effect. Here is the fading effect for your convenience:
$(document).ready(function () { $('#cpass').click(function () { $('#form2,#form3,#form4').hide(); $('#form1').fadeIn(); }); $('#cinfo').click(function () { $('#form1,#form3,#form4').hide(); $('#form2').fadeIn(); }); $('#cmail').click(function () { $('#form4,#form2,#form1').hide(); $('#form3').fadeIn(); }); $('#cplace').click(function () { $('#form3,#form2,#form1').hide(); $('#form4').fadeIn(); }); });
Here is the fiddle for this.
If you really want to keep the general structure of your code here:
$('#cpass').click(function () { $('#form1').collapse('show'); if ($('#form2,#form3,#form4').is(':visible')) $('#form2,#form3,#form4').collapse('hide'); }); $('#cinfo').click(function () { $('#form2').collapse('show'); if ($('#form1,#form3,#form4').is(':visible')) $('#form1,#form3,#form4').collapse('hide'); }); $('#cmail').click(function () { $('#form3').collapse('show'); if ($('#form4,#form2,#form1').is(':visible')) $('#form4,#form2,#form1').collapse('hide'); }); $('#cplace').click(function () { $('#form4').collapse('show'); if ($('#form3,#form2,#form1').is(':visible')) $('#form3,#form2,#form1').collapse('hide'); });
Here is the fiddle .
source share