I have an EventListener that references an Id and it works well, the only problem is that i have at least a dozen places where this EventListener needs to reference so i dont want to have a dozen scripts that are all the same but have a different Id. Is therea way to have the EventListener that references a class that i could use in all the places i need it. Thanks
JAVASCRIPT:
document.getElementById("chartJump").addEventListener("click", function (e) { e.preventDefault() });
HTML:
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenuGraphOne">
<!--DROPDOWN MENU-->
<li role="presentation"><a role="menuitem" tabindex="-1" id="chartJumpOne" href="#graphOneChart">Chart</a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" id="tableJumpOne" href="#graphOneData">Data</a></li>
</ul>
Yes, using classes. Every element should have a common class. And with jQuery you can do this way:
Get more info about
$.click()
and$.ready()
. As you have added jquery, I have given the jQuery solution.Using vanilla JavaScript, you can achieve the same functionality in two ways:
For old browsers:
For new browsers: