0

I am trying to put some HTML in an array for an ajax query, but when I see array in console then there is only first line of the html. why ? is there a proper way to do this?

My code

    var data = new Array();
        $('.get_html').each(function() {
            var html = $(this).html();
            data.push(html);                
        });
   console.log(data);
8
  • Can you create fiddle? Commented Oct 5, 2013 at 4:12
  • Yeah it looks like you want to log data instead of content. Commented Oct 5, 2013 at 4:15
  • see jsfiddle.net/arunpjohny/YMaSJ/1 Commented Oct 5, 2013 at 4:15
  • 1
    this can be easily rewritten as var data = $('.get_html').map(function () { return $(this).html();//return this.innerHTML }).get(); see jsfiddle.net/arunpjohny/YMaSJ/2 Commented Oct 5, 2013 at 4:17
  • console.log(content);change in console.log(data); Commented Oct 5, 2013 at 4:18

3 Answers 3

2

You're logging the variable content instead of the data array you're pushing html too.

console.log(data);
Sign up to request clarification or add additional context in comments.

Comments

2

Change variable content to data in console.log

var data = new Array();
    $('.get_html').each(function() {
        var html = $(this).html();
        data.push(html);                
    });
console.log(data);

JSFIDDLE

Comments

1

You need to use data instead of content, Also you can use .map() to do this

var data = $('.get_html').map(function () {
    return $(this).html();//return this.innerHTML
}).get();
console.log(data);

Demo: Fiddle

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.