Search code examples
jqueryjsonfunctionapinoreturn

JQuery - making a function


I´m new to JQuery and would like to make a function of the following code, I need to use it in different places... Please help me to understand how to make a function in JQUERY.

         var jqxhr = $.getJSON( 'https://api.breeze.pm/projects/'+selected_project_id+'/cards.json?'+token, function(data) {
            console.log( "success 2" );
            //alert(JSON.stringify(json));

            var html = '';
            var len = data.length;

           for (var i = 0; i<len; data[i++]){
              var len2 = data[i].cards.length;
              lid = data[i].id;
              for(var j= 0; j< len2; j++){
                html += '<option value="' + data[i].cards[j].id + '">' + data[i].cards[j].name + '</option>';

              }
            }

            // append(); adds data to list, html(); replaces data on list.
            //  $('select#cards').append(html);
            $('select#cards').html(html);
          })
            .done(function() {
              console.log( "second success" );
            })
            .fail(function() {
              console.log( "error" );
            })
            .always(function() {
              console.log( "complete" );
            });


  })

Solution

  • You can use a normal javascript function like this:

    function doMyJQueryStuff() {
      //Insert your Jquery code here
    }
    

    Then you can call it elsewhere in javascript like this:

    doMyJqueryStuff();