www.webdeveloper.com
Results 1 to 3 of 3

Thread: instanceof HTMLDivElement

  1. #1
    Join Date
    Jun 2012
    Posts
    4

    Question instanceof HTMLDivElement

    Hi there,

    I'm very new to Javascript and am just playing around with some coding (loving it so far). However, I've hit a bit of a snafu:

    Code:
    myDiv = document.getElementById("parentDiv");
    
    for ( i = 0; i < myDiv.childNodes.length; i++ ) {
    
       with (myDiv.childNodes[i]) {
       
          document.writeln(nodeType + "<br>");
          document.writeln(nodeName + "<br>");
          document.writeln(nodeValue + "<br>");
    			
          document.writeln(this instanceof HTMLElement);
          document.writeln(this instanceof HTMLDivElement);
    			
          if ( nodeType == 1 ) {
             var id = getAttribute("id");
    	 document.writeln(id + "<br>");
    	 document.writeln(className + "<br>");
          }
       }
    }
    The nodeType/nodeName printed are:

    3, #text
    1, DIV
    3, #text
    1, DIV
    3, #text

    That's correct. However, what is puzzling me is that all five of these return true for "this instanceof HTMLElement" and all five return false for "this instanceof HTMLDivElement".

    My understanding was that the #text ones would return false for both and the DIV ones would return true for both.

    What am I missing here?

    Thanks,

    Will

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    In your code this is a reference to window, not an HTML element.

  3. #3
    Join Date
    Jun 2012
    Posts
    4

    Question

    Oh, okay. So if I've got this:

    Code:
    with (myDiv.childNodes[i]) {
       document.writeln(nodeType + "<br>");
       document.writeln(this instanceof HTMLElement);
    }
    How would I fix the code to do what I want while still utilizing the with () statement?

    Thanks,

    Will

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles