How can i make this shorter?

Discussion in 'JavaScript' started by baris22, Oct 30, 2010.

  1. #1
    hello all,

    How can i make this shorter? I need to have 15 altogether.

    Thanks all.

    
    
    <script type="text/javascript">
        $(document).ready(function(){
         $("#divextra1").css("display","none");
          $("#divcheckme1").click(function(){
            if ($("#divcheckme1").is(":checked"))
            {
                $("#divextra1").show("fast");
            }
            else
            {      
                $("#divextra1").hide("fast");
            }
          });
        });
    
        $(document).ready(function(){
         $("#divextra2").css("display","none");
          $("#divcheckme2").click(function(){
            if ($("#divcheckme2").is(":checked"))
            {
                $("#divextra2").show("fast");
            }
            else
            {      
                $("#divextra2").hide("fast");
            }
          });
        });
    	
        $(document).ready(function(){
         $("#divextra3").css("display","none");
          $("#divcheckme3").click(function(){
            if ($("#divcheckme3").is(":checked"))
            {
                $("#divextra3").show("fast");
            }
            else
            {      
                $("#divextra3").hide("fast");
            }
          });
        });
    
        $(document).ready(function(){
         $("#divextra4").css("display","none");
          $("#divcheckme4").click(function(){
            if ($("#divcheckme4").is(":checked"))
            {
                $("#divextra4").show("fast");
            }
            else
            {      
                $("#divextra4").hide("fast");
            }
          });
        });
    
        $(document).ready(function(){
         $("#divextra5").css("display","none");
          $("#divcheckme5").click(function(){
            if ($("#divcheckme5").is(":checked"))
            {
                $("#divextra5").show("fast");
            }
            else
            {      
                $("#divextra5").hide("fast");
            }
          });
        });
    </script>
    
    
    HTML:
     
    baris22, Oct 30, 2010 IP
  2. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
  3. Pedro Gorrin Diaz

    Pedro Gorrin Diaz Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    <script type="text/javascript">

    function showHide(obj1,obj2)
    {
    if ($("#"+obj1).is(":checked"))
    {
    $("#"+obj2).show("fast");
    }
    else
    {
    $("#"+obj2).hide("fast");
    }
    }
    </script>

    call the showHide function as follow

    let say

    <input id="divcheckme1" name="divcheckme1" onclick="showHide('divcheckme1','divextra1')/>
    <input id="divcheckme2" name="divcheckme2" onclick="showHide('divcheckme2','divextra2')/>
    <input id="divcheckme3" name="divcheckme3" onclick="showHide('divcheckme3','divextra2')/>

    as follows
     
    Pedro Gorrin Diaz, Oct 31, 2010 IP