Reputation: 1290
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
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
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