0

Failed Example

Original Example

Can anyone tell me how to pass the draggable function as a parameter for clickit, which is an AJAX function? I want to pass draggable as a parameter to attach it to a dynamically added element. I can't get it to work defining the draggable function and pass it to clickit.

function clickit(fun){
  $.ajax({
        'url' : "url",
        'dataType' : 'json',
        'success' : function(data){
         var item_html ="";
            $.each(data.query.results.json,function(i,k){
                item_html += '<div class="dialog"><h3>'+k+'</h3></div>'
            });           
            $('.area').html(item_html);
            fun;         
          }
   });   
}
$('button').click(function(){
    var funpara = $('.dialog').draggable();
    clickit(funpara)
});

Here's the one that works:

function clickit(){
  $.ajax({
        'url' : "url",
        'dataType' : 'json',
        'success' : function(data){
         var item_html ="";
            $.each(data.query.results.json,function(i,k){
                item_html += '<div class="dialog"><h3>'+k+'</h3></div>'
            });           
            $('.area').html(item_html);
            $('.dialog').draggable();         
          }
   });   
}

$('button').click(function(){
    clickit()
});

It looks like the parameter fun cannot be passed to the success function.

0

1 Answer 1

3

It looks like you want to invoke fun, but currently you're just referencing to it without the invocation ()

function (data) {
    var item_html = "";
    $.each(data.query.results.json, function (i, k) {
        item_html += '<div class="dialog"><h3>' + k + '</h3></div>';
    });           
    $('.area').html(item_html);
    fun();         
}

In your context, it looks like you want to invoke $('.dialog').draggable(), so fun should look more like this

function fun(o) {
    return $('.dialog').draggable(o);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.