0

I'm having trouble trying to get a div to get content from another div on the first click.

Here is a bit of my code:

<a href="#" onClick="goPage1();">Feed</a>

And the function it's calling:

function goPage1(){
    $("#feed").html("");
    get_jsonp_feed();
    $("#content").html($("#feedHTML").html());
}

What I'm trying to accomplish is clear the 'feed' div, then call an ajax function that retrieves some data from a database and puts it into the 'feed' div. After that, all the content within the 'feed' div is placed into the 'content' div.

If I don't clear the div in the first place, it displays the data after the second click, but keeps adding the 'feed' div with content that is already there.

Any ideas?

2 Answers 2

1

Your ajax call get_jsonp_feed(); isn't finished before you set the html, ajax queries are asynchronous and take time to execute. They do not wait to finish before the next command is executed.

You need to use a callback function that occurs after the ajax call has returned.

Sign up to request clarification or add additional context in comments.

Comments

0

Pretty hard to say without knowing what's inside get_jsonp_feed, but I imagine your get_jsonp_feed is not completed by the time you are trying to access the data.

// adding the string callback=? to your query tells jQuery
// that it needs to be using JSONP
$.getJSON( "path/to/db.php?callback=?", function( data ) {
    $("#feed").html(data.your_json_members);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.