-2

I am trying to make a pull down menu which gets displayed and vanishes on a click.

JSFiddle

JavaScript

function visible(x) {
    var apple = document.getElementById('pulldown'+x);
    if (apple.style.display = "none")
    {
        apple.style.display = "block";
    }

}

This is working fine, but upon adding this to the above code -

else {
     apple.style.display = "none";
}

The onclick event only works once.

Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
user3788040
  • 361
  • 2
  • 4
  • 11

2 Answers2

0

use == inside the if condition rather than =

if (apple.style.display == "none")
Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
Diego López
  • 1,559
  • 1
  • 19
  • 27
  • You should use `===`. [See this SO question](http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons). `==` does a type conversion so `'0' == 0` is true. However, you (almost) always want to use `===` which will give `'0' === 0` is false. – Sumner Evans Mar 11 '15 at 01:40
0

how about achieve it with the power of jquery?

first we have element (anchor tag) that serve as a trigger and then the second element is the menu which hidden

html

<a href="#" class="pulldowntrigger">Pull down</a>
<div class="themenu" style="display: none;">menu content here</div>

jquery

// when click the anchor tag with a class pulldowntrigger
$('.pulldowntrigger').click(function(){
// check if the class themenu is hidden
if ($(this).next('.themenu')).is(":hidden"){
// yes its hidden! then slide it down (show)
$(this).next('.themenu').slideDown();
}else{
//nah! its not, then slide it up (hide)
$(this).next('.themenu').slideUp();
}
});
Juliver Galleto
  • 8,831
  • 27
  • 86
  • 164