I think you mean that you want the onclick event to change the class.
Here is the answer if someone visits this question and literally wants to assign a class, and it will be enabled using JQUERY.
This is somewhat inconsistent, but if you want to change the onclick event by changing the class, you need to declare the onclick event to apply to the elements of the parent object.
HTML
<div id="containerid"> Text <a class="myClass" href="#" />info</a> Other Text <div class="myClass">other info</div> </div> <div id="showhide" class="meta-info">hide info</div>
Ready for document
$(function() { $("#containerid").on("click",".myclass",function(e){ } $("#containerid").on("click",".mynewclass",function(e){ } $("#showhide").click(function() {changeclass()} });
A little tweak to your javascript
<script> function changeclass() { $(".myClass,.myNewClass").toggleClass('myNewClass').toggleClass('myClass'); } </script>
If you cannot reliably identify the parent, you can do something like this.
$(function() { $(document).on("click",".myclass",function(e){} $(document).on("click",".mynewclass",function(e){} });
If you just want to hide the elements, it might be easier for you to use .hide () and .show ().
source share