problem with document.getElementByTagName

Discussion in 'JavaScript' started by minhazikram, May 20, 2012.

  1. #1
    am not clear with the following line of code

    suppose we have a xml file containing :

    <label>text</label>

    // end of xml file

    allLinks = xhr.responseXML.getElementsByTagName("label")[0].firstChild;

    my question is why are we using the [0] . what is the function of [0]. ?
     
    Solved! View solution.
    minhazikram, May 20, 2012 IP
  2. #2
    the square brackets indicate that this is an array. Arrays are numbered from 0 and not 1. So in this case you use [0] to get the first instance of the label element.

    if you have 3 label elements you would use [1] to get the second and [2] to get the third element.

    hope this helps.
     
    stephan2307, May 21, 2012 IP