Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Javascript DOM to get first element with tag name

Im Trying to get the first element with the tag <figure> using the getElementByTagName().firstChild[0].innerHTML but I run into some problems when adding it to the content, i get undefined.

Here is the javascript that I'm using.

function loopImage()
{
    var value = document.getElementById("loopvalue").value; 
    var content = document.getElementById("contained");
    if(value >= 1)
    {
    content.innerHTML = '<br><input type="button" value="Search" onClick="loopImage()"><input type="text" id="loopvalue"><br><br>';
    for(var i =0;i< value; i++)
    {
        content.innerHTML = content.innerHTML + content.getElementByTagName('figure').firstChild[0].innerHTML;
    }
    content.innerHTML = content.innerHTML + '<style>figure:nth-of-type(2n+2){background:black;color:red;}</style>';
    }
    else if(value == "") location.reload();
    else if(value <= 0) alert("Positive Numbers only!");
    else alert("Are you sure that was a number?")
}

And here is the HTML that I'm trying to get the content from,

<div class="container" id="contained">
    <br>

    <input type="button" value="Search" onClick="loopImage()">
    <input type="text" id="loopvalue">


<br><br>
<figure>
    <a href="photos/DSC01049.JPG">
        <img src = "photos/DSC01049.JPG" alt="City view" height="200" width="200">
    </a>
    <figcaption>City View</figcaption>
</figure>

<figure>
    <a href="photos/DSC01066.JPG">
        <img src = "photos/DSC01066.JPG" alt="City view" height="200" width="200">
    </a>
    <figcaption>Ferris Wheel</figcaption>
</figure>

<figure>
    <a href="photos/DSC02511.JPG">
        <img src = "photos/DSC02511.JPG" alt="City view" height="200" width="200">
    </a>
    <figcaption>Old Mates House</figcaption>
</figure>

<figure>
    <a href="photos/DSC03810.JPG">
        <img src = "photos/DSC03810.JPG" alt="City view" height="200" width="200">
    </a>
    <figcaption>City Skyline</figcaption>
</figure>

<figure>
    <a href="photos/DSC05750.JPG">
        <img src = "photos/DSC05750.JPG" alt="City view" height="200" width="200">
    </a>
    <figcaption>Beach</figcaption>
</figure>


</div>
like image 350
ThomasMcDonald Avatar asked Dec 01 '22 00:12

ThomasMcDonald


1 Answers

Two issues with the code you've quoted:

content.getElementByTagName('figure').firstChild[0].innerHTML
// Missing s -----^                  ^^^^^^^^^^^^^^--- there is no firstChild on the return value

So in theory it could be

content.getElementsByTagName('figure')[0].innerHTML

...but that gets a list and then takes the first one from it, when you just want the first one and don't need the list at all. I'd probably just get the first one:

content.querySelector('figure').innerHTML

querySelector finds the first matching element for a CSS selector, or null if none matches. It's supported on all modern browsers, and also IE8. (It's a relative of querySelectorAll, which returns a list of matching elements for a CSS selector.)

like image 67
T.J. Crowder Avatar answered Dec 05 '22 04:12

T.J. Crowder