javascript - Why does this js not working? Change onclick attribute -


fast , easy problem solve.

function opennav() {     document.getelementbyid("mynav").style.visibility = "visible";     document.getelementbyid("mynav").style.opacity = "1";     document.getelementbyid("nav-icon3").attribute("onclick","closenav()"); } 

this function work fine visibility , opacity changing onclick function not working. ideas? page: http://test.advermedia.pl/

firstly, it's setattribute(), not attribute(). secondly, that's pretty moot anyway using event attributes, let alone dynamically changing them @ runtime, bad idea. use single unobtrusive event handler instead:

<div id="nav-icon3">toggle nav</div> 
document.getelementbyid("nav-icon3").addeventlistener('click', function() {   var nav = document.getelementbyid("mynav");   nav.style.visibility = nav.style.visibility == 'visible' ? 'hidden' : 'visible';   nav.style.opacity = nav.style.opacity == '1' ? '0' : '1'; }); 

or alternatively, you've tagged jquery:

$('#nav-icon3').click(function() {   $('#mynav').toggle(); }); 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -