Reputation: 4212
I want to do ajax call on window load and use a url which is inside div#links
This is what I've come up with so far:
$(window).load(function() {
baseUrl = $("#links a").attr("href");
$.ajax({
url: baseUrl,
type: "get",
dataType: "",
success: function(data) {
// from here not important
HTML:
<div id="links"></div>
Links are created dynamicly:
$('<a href="' + text + lnk + '">' + text + lnk + '</a>')
.appendTo($('#links'));
However this doesn't seem to work.
Upvotes: 0
Views: 152
Reputation: 9975
Looks like you're just not waiting for your #links
div to be filled with content.
The first ajax call fills the links div with its content. On window.load your ajax call hasn't finished yet but you try to access these links already.
You need to execute that code when the first ajax call's success handler is fired. Now the links div doesn't contain anything yet so trying to get an a
tag's href from an a
tag that isn't there yet fails.
Upvotes: 1
Reputation: 6216
updated
it should be
var baseUrl = ...
(for proper scoping)
but that's not the real problem... looks like (for a start) at the the line
$foop = $('<form>' + data.responseText + '</form>');
data.responseText is an entire html document (which this line of code then attempts to wrap with a form element) - so that'll be a major source of issues....
Upvotes: 3