Youss
Youss

Reputation: 4212

Refer to a href inside div

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

Answers (2)

Asciiom
Asciiom

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 atag's href from an atag that isn't there yet fails.

Upvotes: 1

Nathan
Nathan

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

Related Questions