I am working on a multiple page e commerce website with many functions. I want to use functions in JavaScript but I am not able to figure out weather I should go inline like
<div onclick="foo()">
or directly in the script file using eventlisteners
or
using document.getElementById('bar').onclick = function
some people have told me using inline functions isnt a best practice and the problem I am facing is that whenever I use el.addeventlistener()
or el.onclick
inside a js file, it throws an error on other page where the element is not present.It works fine with $('.bar').click(function(){})
with jQuery but i dont want to use jquery only for this purpose.
can someone guide me here please!?