Qiao
Qiao

Reputation: 17049

jQuery getting just added by ajax element

$.post('includes/script.php', $(this).serialize(), function(data) {
    $('body').append(data);
});

alert ($('#new').length)

php script is <php echo "<div id="new">text</div>" ?>

it alerts 0, so it can't see new div.

How can you make it see new div?

Upvotes: 2

Views: 298

Answers (2)

PetersenDidIt
PetersenDidIt

Reputation: 25620

You have to wait till your post is done before you can check the length. Do something like this:

$.post('includes/script.php', $(this).serialize(), function(data) {
    var newItem = $(data).appendTo('body');
    alert (newItem.length);
});

Upvotes: 1

harpo
harpo

Reputation: 43168

How are you calling it? I get alert 1 in Firefox, Chrome, and IE. Note that "function" was misspelled in your code snippet.

<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript">

$(document).ready( function() {
addnew();
});

function addnew (){
    $('body').append('<div id="new">text</div>');
    alert ($('#new').length)
}
</script>

Upvotes: 0

Related Questions