0

Im completely new to Javascript, this is what i want: Guy clicks on an element, so i trigger an onclick and i want to run a JS function, all clear so i need a JS function, what this function needs to do:

  • Check if the display of element #mobilemenu is block or none.
  • When it is block change it to none, when its none change it to block.

What i found so far was this:

function Change(){
document.getElementById("mobilemenu").style.display = "block"; }

But i am stuck on checking if it is currently block or none. I am kinda new to JS so maybe it is super easy (as i think) but i can't find a proper tutorial or some examples.

Thanks in advance!

Yerko Palma
  • 12,041
  • 5
  • 33
  • 57
Chiel
  • 83
  • 10
  • Possible duplicate of [stackoverflow question][1] [1]: http://stackoverflow.com/questions/4866229/can-you-check-an-objects-css-display-with-javascript – Ganesh Salunkhe Mar 03 '15 at 11:51

5 Answers5

0

Just add an if-else.

function Change(){
    var displayVal = document.getElementById("mobilemenu").style.display;

    if (displayVal == "block")
        document.getElementById("mobilemenu").style.display = "none";
    else if (displayVal == "none")
        document.getElementById("mobilemenu").style.display = "block";
}

Here is a Fiddle: http://jsfiddle.net/vaa9goLe/

Anupam Basak
  • 1,503
  • 11
  • 13
0

You can use an if/else statement to check whether the element is displayed, and then show or hide it accordingly:

function Change() {
    /* Put the mobilemenu into a variable */
    var mobilemenu = document.getElementById("mobilemenu");

    /* Check the display property of the element's style object */
    if (mobilemenu.style.display !== "block") {
        /* The element isn't display: block; so show it */
        mobilemenu.style.display = "block"; 
    } else {
        /* The element is display: block; so hide it */
        mobilemenu.style.display = "none";
    }
}
Olly Hodgson
  • 15,076
  • 3
  • 40
  • 60
0

Also, you can use getComputedStyle in case your element doesn't have initial display value to ensure your function will always work.

var element = document.getElementById('btn');

element.onclick = function() { 
    var mydiv = document.getElementById('mydiv'),
        isVisible = (mydiv.currentStyle || window.getComputedStyle(mydiv, '')).display == 'block'; 
    if (isVisible){
        mydiv.style.display = 'none';
    } else {
        mydiv.style.display = 'block';
    }
};

jsfiddle:

http://jsfiddle.net/ykypcmt2/

Ivan Sivak
  • 7,178
  • 3
  • 36
  • 42
0

This may help you

<div id="mobilemenu" style="display:block"></div>
<script type="text/javascript">
function Change(){
var contentId = document.getElementById("mobilemenu");
contentId.style.display == "block" ? contentId.style.display = "none" : 
contentId.style.display = "block";
}
</script>
Priyank
  • 3,778
  • 3
  • 29
  • 48
0

If the div had style value anything other than block or none , the following code should do nothing and preserve the original style value.

function Change(){

        document.getElementById("mobilemenu").style.display = ( document.getElementById("mobilemenu").style.display === "block" ) ? "none" :  ( document.getElementById("mobilemenu").style.display === "none" ) ? "block" : document.getElementById("mobilemenu").style.display ;
}
Md. Arafat Al Mahmud
  • 3,124
  • 5
  • 35
  • 66