Prototype: I'm unable to get the text from a div - I've used both innerHTML and DOM access methods, yet neither work

Posted on 2006-04-06
Last Modified: 2007-12-19
If you go to this page and scroll down a bit:

You'll see a red box with text in it. The box is narrow and tall. You can see the text with your eyes. This box is created dynamically, as you can see in the code below.

When I try to get the contents of the box, I get nothing. I've tried using innerHTML and I get a blank. I've also tested for childNodes and I get zero. You can see the zero and the blank in the final alert() that I call. What might be the right way to access the contents of this box?

Here is the function I call on the page:

function getPopularTagsFromAccumulist() {
  // 04-06-06 - when people enter a new item into or we want
  // the form to auto-suggest to them what some likely categories might be. So, for instance,
  // on this page:
  // where the visitor might type in "General Category" we want to suggest something in the same
  // way that flickr and do.
  // The first step in the process is to get the most popular tags in the Accumulist database. We
  // will use Prototype to fetch this info for us from this page:
  // but even before that, we must create a place where the result can be stored. We don't need
  // to make this container visible on screen.
  var containerForPopularTagsTextArea = document.createElement("div"); = 'output-div'; = "#944"; = "800px;"; = "200px;";
  var refToBody = document.getElementById("main");
  var url = "";
  var pars = 'whatPage=showTagCloudWithCommas&howManyTagsToGet=200';
  var target = 'output-div';
  var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
  var refToTagDiv = document.getElementById("output-div");
  var nodeNumber = refToTagDiv.childNodes;
  var contentsOfContainer = refToTagDiv.innerHTML;
  var containerValue = refToTagDiv.value;
  alert ("the number of nodes: " + nodeNumber.length + " and the wrods: "   + contentsOfContainer  + containerValue);

Question by:lkrubner

    Accepted Solution

    Okay, I figured this one out myself - I was looking for the answer before the answer had returned from the server. With this asynchronous stuff, it is important to make sure the call to the server has returned, before you go looking for some value.
    LVL 75

    Expert Comment

    by:Michel Plungjan
    You may ask for a delete and refund here:

    Featured Post

    Training Course: Android App Development

    This course will involve creating widgets, customize list view, grid view, spinners, etc. Creating applications using audio, video, and SQLite database. Ending with publication on Google Play.

    Join & Write a Comment

    A common challenge, or question, when working with two calendar dates is: What is the difference between the specified days? As is frequently the case, the answer is: It depends... What kind of difference do you want? Given two date obje…
    Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now