javascript - Creating anonymous functions inside conditional statements -


i've snippet :

if(el) {     el.addeventlistener("click", function(){         if (body.classlist.contains("el-is-open")) {             body.classlist.remove("el-is-open");         } else {             body.classlist.add("el-is-open");         }     });      foo.addeventlistener("click", function(){         body.classlist.remove("el-is-open");     }); } 

is okay wrap these statements inside condition? i'm new js i'm not sure if okay do..

before got:

uncaught typeerror: cannot read property 'addeventlistener' of null 

after can have js inside same file, , run if document have id el (i.e). thanks


Comments

Popular posts from this blog

C# random value from dictionary and tuple -

cgi - How do I interpret URLs without extension as files rather than missing directories in nginx? -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -