javascript - Load AJAX function value dynamically -


i need insert list of string dynamically created ajax call. function:

function finingredients(boxid, prodid) {         $.ajax({             type: "get",             url: "/products/getboxingredientsnew?idbox=" + boxid,             datatype: "json",             contenttype: "application/json; charset=utf-8",             error: function () {                 alert("error");             },             success: function (data) {                 if (data != "error") {                             var element =                             $('<li> ' + item[0] + '<span class="new-text-intro"> (' + item[1] + ' kg)</span> omaggio</li>');                             $("#ingredients").append(element);                         }                     });                 }             }         });     } 

this function works fine , returns correct value. must insert result of function <ul> tag "ingredients":

@foreach (var item in model)    {       <h2 class="sottotitolonostribox">@item.productcartdesc</h2>       <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">          <ul class="new-text-intro2" style="min-height: 154px;" id="ingredients"  ondblclick="trovaingredients(@item.productid,@item.productid);"></ul>       </div>    } 

the problem want insert value of functions each element when load it. code works fine when double-click on each element.

if code works when click on element, sounds you've put function in "onclick" event,

just try calling function in "document ready" event after function definition:

$(document).ready(function(){     var = ...     var b = ...     finingredients(a,b); }); 

Comments