Why doesn’t it work?

Asked

Viewed 36 times

0

I’m new to messing with JS and it seems a silly mistake, but I caught it...

When I try to use the code as follows, it doesn’t work...

So it doesn’t work

<div id="intfloat" onclick="Showmenu()" class="intfloat">
<div id="intfloatM" class="intfloatM"></div>
<div id="intfloatN" class="intfloatN"></div>
<img class="ifimg" src="static/img/others/intfloat.png"/>
</div>
<script>

    function Showmenu() {
        var display = document.getElementById('intfloatM').style.display;
        if(display == "none")
            document.getElementById('intfloatN').style.display = 'block';
            document.getElementById('intfloatM').style.display = 'block';
        else
            document.getElementById('intfloatM').style.display = 'none';
            document.getElementById('intfloatN').style.display = 'none';
    }
    </script>

But if I only get one DIV it works

that’s how it works

function Showmenu() {
        var display = document.getElementById('intfloatM').style.display;
        if(display == "none")
            document.getElementById('intfloatM').style.display = 'block';
        else
            document.getElementById('intfloatM').style.display = 'none';
    }

Is there any restriction ? Or is something wrong ? ;-;

  • 3

    Dude, I don’t know exactly why you’re doing it this way, but wouldn’t you like to try the toggleClass? create an Hidden class and a show and then change the class with . toggleClass()?

  • 2

    The keys are missing in your if/Else, so only the first line of each block is really conditional, the second lines always run. And check whether style.display == 'none' will give problem the first time, because the value in this case will be ''. I suggest trying @flourigh’s suggestion.

  • I understood, it was a silly mistake the lack of the keys "{}", with them worked normally, obg personal

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.