What you have coded doesn't work because the $.get call is *asynchronous*.
When you call $.get, $.post, $.ajax or any async function you pass a
callback function.  The actual $.get call returns immediately, but the
results from the server aren't passed to your callback method until the
server responds.  That may take a few hundred milliseconds or it may be many
seconds.  You have no control over when the response is received so you need
to structure your code with that in mind.

On Feb 15, 2008 8:35 PM, jquertil <[EMAIL PROTECTED]> wrote:

>
> I know little about variable scope, have been reading up on OOP, but I
> don't understand why this would not work.
> I tried inserting "return objectArray" in various places to no
> avail...
>
> $('#button1').click(function(b){
>    $().loadXML2array('url','var');
>    alert(objectArray);               // does not work
> });
>
>
> jQuery.fn.extend({
>    loadXML2array: function(url,variable) {
>        return this.each(function() {
>            $.get(url,{xml:variable}, function(data){
>                 objectArray = new Array();
>                 $(data).find('row').each(function(i){
>                     rowObj = new Object();
>                     rowObj.col1 = $(this).children('col_1').text();
>                     rowObj.col2 = $(this).children('col_2').text();
>                     rowObj.col3 = $(this).children('col_3').text();
>                     objectArray.push(rowObj);
>                });
>            });
>        });
>    }
> });
>

Reply via email to