Using a variable created within an AJAX request in another function

Asked

Viewed 900 times

1

I have a list of projects where, when the Thumb of a project is clicked, the script loads images related to the project.

$('.project-grid').on('click', 'a', function (event) {
    event.preventDefault();
    var $this = $(this);
    var id_projeto = $this.data('projeto');
    pega_imagens(id_projeto);
});

The last line calls the following function:

function pega_imagens(id_projeto){
    var id_projeto = id_projeto;

    $.ajax({
        type: 'POST',
        data: ({ id_projeto: id_projeto }),
        dataType: 'json',
        url: 'php/imagens.php',
        success: function (data) {
            var toAppend = '';
            toAppend += '<div class="gutter-sizer"></div><div class="grid-sizer"></div>';
            for (var i = 0; i < data.length; i++) {
                toAppend += '<img class="item" src="img/' + data[i].imagem + '" width="' + data[i].largura + '" height="' + data[i].altura + '" />';
            }

            $('#project-expander-images-'+id_projeto).append(toAppend);
            alturaTotal = $('#project-expander-images-'+id_projeto).height();
        },
        error: function (data) {
            console.log('erro' + data);
        }
    });
}

My question is this: how do I use the variable alturaTotal out of function pega_imagens? I know I must use a function callback, but I don’t know how to include it in the code.

UPDATE

I made some adjustments to the code, putting all the actions that were on success in the callback function:

function pega_imagens(id_projeto, callback){
    var id_projeto = id_projeto;

    $.ajax({
        type: 'POST',
        data: ({ id_projeto: id_projeto }),
        dataType: 'json',
        url: 'php/imagens.php',
        success: function(data){
            foo(data, id_projeto);
        }, 
        error: function (data) {
            console.log('erro' + data);
        }
    });
}

 function foo(data, id_projeto){
    var toAppend = '';
    toAppend += '<div class="gutter-sizer"></div><div class="grid-sizer"></div>';
    for (var i = 0; i < data.length; i++) {
        toAppend += '<img class="item" src="img/' + data[i].imagem + '" width="' + data[i].largura + '" height="' + data[i].altura + '" />';
    }

    $('#project-expander-images-'+id_projeto).append(toAppend);
    alturaTotal = $('#project-expander-images-'+id_projeto).height();
    console.log(alturaTotal);
 }

But the value returned by the variable alturaTotal is incorrect.

  • Hello @marcelo2605, where exactly do you need to use this value? - would be in the continuity of the first block, after calling the pega_imagens(id_projeto);? if elsewhere, could exemplify?

  • @This is exactly where I’ll use the value.

1 answer

1

I believe that the total height should be obtained only where necessary or even in another function, because it does not seem to be one of the functionalities of the function pega_imagens.

From what I understand, your problem is to execute the code that will use the full height after running ajax. If so, a callback function, as you yourself mentioned, solves the problem.

pega_imagens(id_projeto, function() {
    var alturaTotal = $('#project-expander-images-'+id_projeto).height();
    console.log(alturaTotal);
});

function pega_imagens(id_projeto, callback){
    $.ajax({
        // ...
        success: function() {
            // ...
            if ($.isFunction(callback)) {
                callback();
            }
        }
    });
}
  • Is the code correct? I tried to execute it and appeared Uncaught Referenceerror: id_project is not defined

  • I also think that each thing is one thing. How about just change this line alturaTotal = $('#project-expander-images-'+id_projeto).height();from the second to the first block, placing just below the pega_imagens(id_projeto);. As you have already made the append, you should be able to access the new div.

  • @Gebender Look at the change I made to the code.

  • @Marcelo2605 As for the Uncaught ReferenceError, you placed the function call pega_imagens in the same place as before (at the end of the onclick function)? As for your editing, if the height value is not correct it is for some external reason, the code is correct. Maybe the images are with float. If this is the case, the external div (which you are trying to get the height) will not adjust the images. Read this post to better understand the problem with the float.

  • @Oeslei The images are float. But still I can’t get the height of the div that appears in the DOM?

  • @marcelo2605 You can read the height, but it will be incorrect due to the float. Read the post I gave you to understand better. To solve this problem, use one of the solutions presented in the post or change the style float for display: inline-block.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.