Search code examples
jqueryduplication

Avoid duplication of function in jQuery


This function is written twice to save the checkbox state as well as the corresponding div text. How do I write the code in a function once and then call it twice on load and click events respectively?

$(document).ready(function() {
                    $("#bquote").load("quotes_in.php", function() { 

                    // a key prefix is used for the cookie/storage
                    var storedData = getStorage('com_mysite_checkboxes_'); 

                    $('div.check input:checkbox').bind('change',function(){
                    $('#ab_' + this.id).toggle($(this).is(':checked'));

                    // save the data on change
                    storedData.set(this.id, $(this).is(':checked')?'checked':'not');
                    }).each(function() {

                    // on load, set the value to what we read from storage:
                    var val = storedData.get(this.id);
                    if (val == 'checked') $(this).attr('checked', 'checked');
                    if (val == 'not') $(this).removeAttr('checked');
                    if (val) $(this).trigger('change');

                    });
                 });                 

            });



            $(function() {


            /*load quotes on click of link*/
            $("a#main")
                .click(function() {
                   $(this).addClass("current"); 
                   $("#bquote").load("quotes_in.php", function() {  

                    // a key prefix is used for the cookie/storage
                    var storedData = getStorage('com_mysite_checkboxes_'); 

                    $('div.check input:checkbox').bind('change',function(){
                    $('#ab_' + this.id).toggle($(this).is(':checked'));

                    // save the data on change
                    storedData.set(this.id, $(this).is(':checked')?'checked':'not');
                    }).each(function() {

                    // on load, set the value to what we read from storage:
                    var val = storedData.get(this.id);
                    if (val == 'checked') $(this).attr('checked', 'checked');
                    if (val == 'not') $(this).removeAttr('checked');
                    if (val) $(this).trigger('change');

                        });      
                });
            });

Solution

  • You can declare it once as a named function and call it twice, like this:

    $(function() {
      function loadQuotes() {
        $("#bquote").load("quotes_in.php", function() { 
    
          // a key prefix is used for the cookie/storage
          var storedData = getStorage('com_mysite_checkboxes_'); 
    
          $('div.check input:checkbox').bind('change',function(){
            $('#ab_' + this.id).toggle(this.checked);
    
            // save the data on change
            storedData.set(this.id, this.checked?'checked':'not');
          }).each(function() {
    
            // on load, set the value to what we read from storage:
            var val = storedData.get(this.id);
            if (val == 'checked') $(this).attr('checked', 'checked');
            if (val == 'not') $(this).removeAttr('checked');
            if (val) $(this).trigger('change');
    
          });
        });                 
      }
      $("a#main").click(function() {
        $(this).addClass("current"); 
        loadQuotes();
      });
      loadQuotes();  //call it once on load
    });
    

    I also changed $(this).is(':checked') to this.checked in the above, no need to make it slower, the DOM property works here :)