0

HTML

   <nav>
    <ul>
        <li><a class="notDrop" href="#home">HOME</a></li>
        <li><a class="notDrop" href="guides.html">GUIDES</a></li>
        <li class="dropdown">
            <a class="dropbtn">BRANDS</a>
                    <div class="dropdown-content">
                    <a href="gaming.html">NVIDIA</a>
                    <a href="#">INTEL</a>
                    <a href="#">CORSAIR</a>
                    <a href="#">SAMSUNG</a>
            <li class="dropdown">
                <a class="dropbtn">BUILDS</a>
                    <div class="dropdown-content">
                    <a href="gaming.html">GAMING</a>
                    <a href="#">OFFICE</a>
                    <a href="#">SERVER</a>
                    <a href="#">MEDIACENTER</a>
                    </div>
            </li>
    </ul>
</nav>

CSS

    nav {
    background-color: white;
    overflow: hidden;
    display: flex;
    justify-content: center;
}

Javascript

    window.onload = function mobile(){
    if( isMobile.any() ) {
        document.getElementById('nav').style.backgroundColor = "blue";
       alert('Mobile'); 
    }
}

But I can't get it to change color, what am I doing wrong? I managed to change the color of the body but I cant of the nav.

8
  • can you share the html for nav bar Commented May 1, 2017 at 14:59
  • Give the nav element an ID and change the getElement call to use that ID. Nav is the element type - not its ID. Commented May 1, 2017 at 15:01
  • 1
    @Leyer What about alert('Mobile');? Is it working fine? Commented May 1, 2017 at 15:03
  • how many nav tag is use id used in the page? Commented May 1, 2017 at 15:04
  • @Archer oh okey, I will try. Thanks Commented May 1, 2017 at 15:05

3 Answers 3

1

You used the wrong javascript function. nav is a tag, not an id.

It should be getElementsByTagName("nav")[0] instead of getElementById("nav")

javascript will always return an array regardless of number of matching element using getElementsByTagName() method. So use [0] to return the first one.

Sign up to request clarification or add additional context in comments.

6 Comments

Is it the first nav element on the page?
Are you sure? He has not added any html
@Mendax Now he has posted his html code. Still I insist this ans will work
This answer needs clarification for people that end up here looking for help. Please explain it, and specifically mention that if they ever add another nav element to the page it may stop this working.
Just another question, if I had css like this: nav a { css in here } how would I put it in the element?
|
0

You used document.getElementById but you have only nav tag from HTML5. Add id = "nav" to your nav tag.

Best regards!

Comments

0

Your problem here is that you are using getElementById, but have not given your nav an id.

The best way to solve this would be to add an id to your nav, as follows:

<nav id="top-nav">

And your js becomes:

document.getElementById('top-nav').style.backgroundColor = "blue";

The answer given by Super Cool Handsome Gel Boy, while it will work is impractical because it looks for any <nav> tag, so if you add more <nav> tags, or move them around, you will have to recode your javascript.

By using an id, the javascript will always target the right <nav>.

Comments

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.