0
var acorn = document.getElementById("boxa");
var outputResults = acorn.childNodes;
document.write(outputResults);

My goal is to write all the child nodes within the id=boxa, that is not working ?

3
Contributors
9
Replies
35
Views
3 Years
Discussion Span
Last Post by Siberian
0

I'm not sure what you're trying to do... here's a JSBin with what you posted and it is creating output, I just don't have any idea what you're grabbing and what result is expected. Maybe some more code would help. Even elaborating on that jsbin would be great^^

0

I want to write out all the tags within the div=boxa, that's all :)
document.write()
What is written - h1,p, whatever etc :)

0

this is a crafty way of doing it...
http://stackoverflow.com/questions/1700870/how-do-i-do-outerhtml-in-firefox

function outerHTML(node){
    // if IE, Chrome take the internal method otherwise build one
  return node.outerHTML || (
      function(n){
          var div = document.createElement('div'), h;
          div.appendChild( n.cloneNode(true) );
          h = div.innerHTML;
          div = null;
          return h;
      })(node);
  }

so in your case, you can do:

var acorn = document.getElementById("boxa");
var outputResults = acorn.childNodes;
for (var i=0; i<outputResults.length;i++)
    document.write(outerHTML(outputResults[i]));
0
var acorn = document.getElementById("boxa");
var outputResults = acorn.childNodes;
for (var i=0; i<outputResults.length;i++)
document.write(outerHTML(outputResults[i]));

The function you posted is a little advanced for me at this time.

[object nodelist] is the result from the script, that doesn't seem right ?

0

It all depends on what your actual code looks like (including the HTML).

Printing out [object nodelist] means the value you are passing into the function is an HTML Collection, which tends to be the return from having multiple items that match a get javascript native function (many browsers return collections when you do getElementById("x") where multiple elements have the id "x".

So, it very well could be "right," but without seeing the whole thing or at least the relevant implementation, so we can follow the code directly and see what you are doing.

0

I want to write out each tag to in order that it's presented in the HTML. First is the ID, second is the H1 iD and so on.

<body>

<div id="boxa">
<h1 id = "header">This is an emergency</h1>
<p>what is going on <span id="goat"></span><p>
<p>Lets all go to the <span id="place"></span></p>
</div>


</body>
0

Dude, at least read what you're doing. You placed Javascript in the HTML block and HTML in the Javascript block.
Also, those type of websites don't allow the use of document.write() I'm not sure why.

Try this one, it seems to be what you where trying to do. JS BIN take a look at the console log to see what's happening.

1

It was a dumb mistake !
The length command, gotta remember that ! Thanks :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.