JavaScript Does getElementsByTagName return ALL child nodes? can getelementsbytagname return null,what does getelementsbytagname return,what does

So let's say we have the following XML structure:

<property>
    <label>Label 1</label>
    <value>value 1</label>
</property>
<property>
    <label>Label 2</label>
    <value>value 2</label>
</property>
<!-- more of these -->

Provided that we have correctly loaded the XML document into variable var xml, does xml.getElementsByTagName("property") return ALL property, label, value nodes OR just property nodes with no children?

Why I'm asking this is, I'd like to be able to do the following:

var props = xml.getElementsByTagName("property");
var labels = props[0].getElementsByTagName("label");

If the function will not return any label or value nodes, what's the best way to get that done?

Answer:1

The getElementsByTagName method “Returns NodeList of all descendant Elements with a given tag name”. So getElementsByTagName("property") returns all children, children of children, etc., that have the tag name property, no matter what the content of such a node is. It does not of course return any node with some different tag name.

So yes, the code

var props = xml.getElementsByTagName("property");
var labels = props[0].getElementsByTagName("label");

does what you seem to want to do: it assigns to labels a (live) list of label children of the first property element in the document, provided that there is at least one property element. (So for robustness, you might wish to check e.g. that props.length > 0 before proceeding to the second statement.)

Answer:2

I had a simple script working using Promises in ECMA6 but I'm now converting what I wrote to jQuery to be compatible for all browsers but the issue I'm having is that availableDevices contains an ...

I had a simple script working using Promises in ECMA6 but I'm now converting what I wrote to jQuery to be compatible for all browsers but the issue I'm having is that availableDevices contains an ...

  1. jquery function after page load
  2. jquery function after delay
  3. jquery function after load
  4. jquery function after function complete
  5. jquery function after .each() has completed
  6. jquery function after function
  7. jquery function after animate
  8. jquery function after append
  9. jquery function after fadeout
  10. jquery function after another
  11. jquery function after timeout
  12. jquery function after time
  13. jquery function after ajax load
  14. jquery function after click event
  15. jquery function after page load complete
  16. jquery function after click
  17. jquery function after load html
  18. jquery function after dom load
  19. jquery function after submit
  20. jquery function after addclass

My application uses iframes to display content. I need to detect whether a page is loaded in an iframe. The problem is I need to do it with php, javascript manipulation of the DOM is not enough, I ...

My application uses iframes to display content. I need to detect whether a page is loaded in an iframe. The problem is I need to do it with php, javascript manipulation of the DOM is not enough, I ...

I'm following the book Getting Started With Meteor and I'm really not getting far because simple errors keep blocking me. At this point in time I've started writing the initial app in the book in ...

I'm following the book Getting Started With Meteor and I'm really not getting far because simple errors keep blocking me. At this point in time I've started writing the initial app in the book in ...

I'd like to get the number of characters between the start of a container and the cursor (including tags characters). Example : <div id="contentEditor" contenteditable="true"> <p>...

I'd like to get the number of characters between the start of a container and the cursor (including tags characters). Example : <div id="contentEditor" contenteditable="true"> <p>...