The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Javascript to Check if Element has Children

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
  • #146931

    I am learning JavaScript and would like to test whether a list in the navigation has a nested list within it and if it doesn’t I would like to change the style on the parent li of that nested list. Confusing?
    Here is what I have so far, I know it doesn’t target the right nodes, but that’s why I am here.

    function hasList() { var nav =document.getElementById("header-nav");
            if (nav.hasChildNodes() ) {
                nav.childNodes[2].setAttribute("style","background-color:red; border-radius:8px;");
    }; };`

    Thanks, but it doesn’t seem to be targeting anything.
    Here is my page.

    I was looking to round the bottom corners of the li’s that didn’t have a nested ul (capabilities and contact). The background-red, was to make it easy to see what was getting targeted.


    Thanks CrocoDillon, I just went with the CSS solution. It was kinda a “duh” moment for me. ;)

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.