Sam Selikoff
Sam Selikoff

Reputation: 12694

Part of jQuery's callback handling undoes my statement

I am using AJAX via jQuery to make a get request. Upon success, I place the result into a div:

$.get("content.php", function(result) {
  $('#content').html(result);

The content I'm requesting itself is HTML and has two divs, #article_name and #article_content. After it loads, I check if #article_content has any children. If it doesn't, I add a <p> block:

  if (!$('#article_content').children().length) {
    $('#article_content').html('<p>(Click here to enter text)</p>');
  }
}

This works, until JavaScript leaves the callback function. That is, after my conditional, the <p> block is added; but as I step through in Chrome's inspector, it dives into jQuery's callback section, and by the time it's through the <p> block disappears.

I've run my conditional in the console to make sure it's not the problem.

Upvotes: 5

Views: 100

Answers (1)

Sam Selikoff
Sam Selikoff

Reputation: 12694

I had the ordering wrong - I placed my conditional before the code that updated the content area.

Should I delete this question?

Upvotes: 0

Related Questions