Szymon
Szymon

Reputation: 1290

jQuery .load() get content without append

I was wondering hwo to make function .load() without append in some selector

For example I'd like to get variables as array from another file which I did thanks to script below. But I have to append it on #selector to retrieve information I want to get. Is there any way to get those information without creating this selector. Of course i can "display: none;" this selector. But I would like to have clean code.

Any ideas would be appreciate :))

$("#selector").load('content/file.php', function (result) {
    var hrefs = new Array();
    $("ul li").each(function () {
        hrefs.push($(this).find('a').attr('href'));
    });
});

Upvotes: 0

Views: 108

Answers (2)

Arun P Johny
Arun P Johny

Reputation: 388396

You can use $.get() to fetch the contents from the said resource then find the ul li elements like

$.get('content/file.php', function (result) {
    var hrefs = $(result).find("ul li").map(function () {
        return $(this).find('a').attr('href');
    }).get();
}, 'html');

No need to use .find() to find the anchor elements, add it to the ul li selector

$.get('content/file.php', function (result) {
    var hrefs = $(result).find("ul li a").map(function () {
        return $(this).attr('href');
    }).get();
}, 'html');

Upvotes: 2

Neel
Neel

Reputation: 11741

try ajax call

var response;
$.ajax({ type: "GET",   
     url: 'content/file.php',
     async: false,
     success : function(text)
     {
          var hrefs = $(text).find("ul li").map(function () {
    return $(this).find('a').attr('href');
}).get();
     }
});

Upvotes: 1

Related Questions