0

I have to retrieve hier2 object in jquery from following HTML code in ASPX page

<div>
<hier2:hierarchy >
 <hier:x1></hier:x1>
<hier2:hierarchy >
</div>

I trier document.all.hier2 in IE 11 and its not working

what is the best approach to do this.

2
  • document.all really? use document.getElementsByTagName or document.querySelectorAll Commented Mar 2, 2015 at 15:42
  • Is there any equivalent code in jquery for document.getElementsByTagName("hier2:hierarchy"); Commented Mar 2, 2015 at 16:10

1 Answer 1

1

simply put:

document.getElementsByTagName("hier2:hierarchy"); 

returns a node list you can loop over containing all the hier2:hierarchy nodes.


More elaborate:

Those elements (or nodes) are part of the Document Object Model (DOM). In the past IE (and others) supported document.all. It's now deprecated. How to traverse the DOM. The DOM is a tree that contains a root and its children, grandchildren, etc.

basics:

  • document: This is the master object. The root of the page. All other elements are a descendant of this root.
  • document.documentElement: representing the HTML-element of the page.

Traversal:

  • document.getElementById: allows you to select a single element based upon its ID-attribute.
  • document.getElementsByTagName: allows you to select multiple nodes based upon the node name.
  • document.getElementsByClassName: allows you to select multiple nodes based upon the node's class.
  • document.querySelector: select a single node using css-selectors
  • document.querySelectorAll: same as the above, only for selecting multiple elements.
  • children or childNodes: a subtle difference, the first selects content nodes, while the latter selects all the nodes that are direct children (in the form of a node list) of an element.
  • parentNode or parentElement: select the parent element of the current element.
  • previousSibling or nextSibling: select the previous or next element.

Many more options here: https://developer.mozilla.org/en-US/docs/Web/API/Element

Some examples:

plain:

var elements = document.getElementsByTagName("hier2:hierarchy");
for (var i = 0; i < elements.length; i++)
  {
    document.querySelector("#display").innerHTML += elements[i].nodeName; //select the display div using css selector #display
  }
<div>
  <hier2:hierarchy>
    <hier:x1></hier:x1>
  </hier2:hierarchy>
</div>
<div id="display"></div>

In jQuery

  $("hier2\\:hierarchy").each(function() {

    $("#display").html(this.nodeName);
  });


//$("hier2\\:hierarchy") is used to select the elements, mind the \\ to escape!
//each is used to traverse all the elements.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <hier2:hierarchy>
    <hier:x1></hier:x1>
  </hier2:hierarchy>
</div>
<div id="display"></div>

Special reminder here: In using hier2:hierarchy will fail. You need to escape the : with \\: to

$("hier2\\:hierarchy")
Sign up to request clarification or add additional context in comments.

1 Comment

Is there any equivalent code in juqery for document.getElementsByTagName("hier2:hierarchy");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.