Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3233300
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:15:40+00:00 2026-05-17T17:15:40+00:00

I have a div which in jquery’s document ready I append – using $(#div

  • 0

I have a div which in jquery’s document ready I append – using $("#div id").append('html text') syntax – with 10 or so more div child elements.

once this is done I try to check the content of the child divs via alert($(".classname")); and it comes back with:

function(a){if(c.isFunction(a))return this.each(function(b){var d=c(this);d.text(a.call(this,b,d.text()))});if(typeof a!=="object"&&a!==w)return this.empty().append((this[0]&&this[0].ownerDocument||s).createTextNode(a));return c.text(this)}

I would have expected it to alert with the html contents of the child divs and not javascript?

full script:

<script type="text/javascript">
    $(document).ready(function(){

        // twitter api's base url
        var url="http://search.twitter.com/search.json?callback=?&result_type=recent&q=";
        // we'll store the search term here
        var query = "blah";
        // get the json file
        $.getJSON(url+query,function(json){
            // this is where we can loop through the results in the json object
            $.each(json.results,function(i,tweet){
                // this is where we do what we want with each tweet
                $("#results").append('<div class="tweetBox"><span class="unseen">'+tweet.created_at+'</span><div class="tweetImg"><img src="'+tweet.profile_image_url+'" width="48" height="48" /><a class="overbox" href="http://twitter.com/'+tweet.from_user+'/status/'+tweet.id+'"></a></div>'+tweet.text+' ...said '+((new Date().getTime()/1000/60)-(new Date(tweet.created_at))/1000/60).toFixed(0)+' minutes ago</div>');

            });
        });

        $("#results").height(function(){return $(window).height()-204;});
        alert($(".unseen").html());     
    });
</script>

<div id="results"></div>

update:
definitely some kind of jquery/javascript race condition going on here, if I replace the alert with setTimeout(function(){alert($(".unseen").html());},1000); it returns the text expected. If I change the timeout pause to 1 millisecond it returns null once again.

Not sure of a ‘real’ workaround for this other than sticking in the delay?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:15:41+00:00Added an answer on May 17, 2026 at 5:15 pm

    Ajax calls (like $.getJSON) are done asynchronously.

    This means that when the jQuery selection is done(at the bottom of the script) the response might not be received yet (still on the way).

    You have to move all the code that depends on elements created from the response in the callback function (right after the $.each(...);)

    Ex:

    $.getJSON(url+query,function(json){
        // this is where we can loop through the results in the json object
        $.each(json.results,function(i,tweet){
            // this is where we do what we want with each tweet
            $("#results").append('<p>this element is created only when the callback is triggered</p>');
        });
        // do stuff here with all your created elements
        alert('found '+$(".unseen").length+' objects');
    });
    

    Also note that the html() function returns the html contents of only the first element in the set.

    EDIT: your timeout trick works because it gives the ajax call the time it needs to complete and trigger the callback function that ads the objects to the DOM.

    See working example here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Div Tag which contains 4 child Div Tags <Div id=Parent> <div
I have a div element which is made jquery Resizable. It has alsoResize option
I have a div which collapses clicking on a text header. I want to
I have a div container inside which there are several child divs with css
in my aspx page i have a div which contains simple html ,has some
I have one aspx page with some controls. Also i have one DIV which
I have a div in which a page is loaded with the DojoX Layout
I have a div with overflow:hidden , inside which I show a phone number
I have an absolutely positioned div containing several children, one of which is a
I have small page which has label, DropDownList and a submit button. <div> <asp:label

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.