xotix
xotix

Reputation: 530

Getting img's src path and convert it to json

I'm getting some html content via ajax and on the callback I'd like to get all src paths and convert them into a json. I'm always getting 404 errors, because it loads the images. How can i prepend it from loading? Also, I'm getting the error: Uncaught TypeError: Converting circular structure to JSON

Here's a demo: http://jsfiddle.net/QgFMe/

The only thing i can think of is regex, but i would prefer a non regex solution.

Thanks

Upvotes: 1

Views: 366

Answers (2)

Alnitak
Alnitak

Reputation: 340055

To fix your JSON error, pass the output of .map() to .get().

This converts the pseudo jQuery object returned by .map() into a real array:

var pics = $(this).find("img").map(function () {
    return this.src;                                                                                                                                          
}).get();

NB: $(this).attr('src') === this.src

I don't know a solution to your 404 error though - see comments above.

Upvotes: 0

benedict_w
benedict_w

Reputation: 3608

Try:

$(document).ready(function () {
    var pics = [];
    $(this).find("img").each(function () {
        pics.push($(this).attr("src"));                                                                                                                                           
    });

    console.log(pics);

    var json_data = JSON.stringify(pics);

    console.log(json_data);
});

Upvotes: 1

Related Questions