javascript - show/Hide menu does not work

Asked

Viewed 104 times

0

function dpdShowHide(){
var orgBtn = getElementById('organizador');
var dpdOrg = getElementById('org-dpd-list');
    if (orgBtn !== false) {
        dpdOrg.visibility('none')
    }
    else{
        dpdOrg.visibility('block')
    }
}

So my problem is this, I’m trying to make a hidden menu (a ul) appear and disappear by clicking on li(parent element of that other ul), but it’s not working

  • 1

    You could also put the HTML code?

1 answer

0


Is this?

<script>
function dpdShowHide(ele){

    dpdOrg = ele.children[0];
    if (dpdOrg.style.display == 'none') {
        dpdOrg.style.display = 'block';
    }
    else{
       dpdOrg.style.display = 'none';
    }
}

var orgBtns = document.getElementsByClassName('organizador');
for (var i = 0 ; i < orgBtns.length; i++) {
    orgBtns[i].addEventListener('click', function() {
       dpdShowHide();
    });
}

</script>

<ul>
       <li>
          li
       </li>
       <li class="organizador" onclick="dpdShowHide(this)">sub ul1
           <ul id="org-dpd-list" style="display: none">
              <li>sub li</li>
              <li>sub li</li>
           </ul>
       </li>
       <li>
          li
       </li>
       <li class="organizador" onclick="dpdShowHide(this)">sub ul2
           <ul id="org-dpd-list" style="display: none">
              <li>sub li</li>
              <li>sub li</li>
           </ul>
       </li>
    </ul>

  • Exactly, thank you man, but I have one more question, as I would for this same code to work in several buttons on the same menu, however, without having to repeat... And I’m sorry I’m wasting your time

  • Can give date attributes, 1 secs I will do

  • Done @Murilogambôa

Browser other questions tagged

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