tags:

views:

37

answers:

1

Hi all,

I have two functions that pulling some of content from html and returning it in body.

$(document).ready(function() {

    var b = [];
    $('.avatar_ name').each(function(i,e) {
        b[i] = $(this).text();
    });

    $('body').empty();
    for (var i = 0, j = b.length; i < j; i++) {
        $('body').append( b[i] + '<br>');
    } 


});
(document).ready(function() {
 var a = [];
    $('a.avatar_user').each(function(i,e) {
        a[i] = $(this).attr('href');
    });

    $('body').empty();
    for (var i = 0, j = a.length; i < j; i++) {
        $('body').append( a[i] + '<br>');
    }
});

What I'm trying to do as a result is to merge those two functions together and as a result get this:

$('body').append( a[i] + b[i] + '<br>');

Any Help much appreciated

Thank you in advance

+2  A: 

ready(fn) is a special function in JQuery that can be written in one or many blocks. If written in many blocks, each block can not access others variables.
In your case it's better to write a single function:

$(document).ready(function() {

    var a = [];
    var b = [];

    $('a.avatar_user').each(function(i,e) {
        a[i] = $(this).attr('href');
    });

    $('.avatar_ name').each(function(i,e) {
        b[i] = $(this).text();
    });

    $('body').empty();
    for (var i = 0, j = a.length; i < j; i++) {
        $('body').append( a[i] + b[i] + '<br>');
    } 
});

(if the 2 arrays have different length, you can add a check before appending to bdy)

najmeddine