1

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

1 Answer 1

3

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)

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.