javascript - Trying to apply classes on a click event using JS -



javascript - Trying to apply classes on a click event using JS -

i'm new javascript , have used primary post how toggle classes here: change element's css class javascript reason code not executing expect to. code placed in bottom of html file.

<script> $(document).ready(function () { $('.arrow').addeventlistener('click', function() { if (this.hasclass('glyphicon-chevron-right')) { this.addclass('glyphicon-chevron-left'); this.removeclass('glyphicon-chevron-right'); } if (this.hasclass('glyphicon-chevron-left')) { this.addclass('glyphicon-chevron-right'); this.removeclass('glyphicon-chevron-left'); } }, false); }); </script>

set click listener , utilize .toggleclass() instead of if clauses:

$('.arrow').on('click', function() { $(this).toggleclass('glyphicon-chevron-right'); $(this).toggleclass('glyphicon-chevron-left'); });

or more succinctly

$('.arrow').on('click', function() { $(this).toggleclass('glyphicon-chevron-right glyphicon-chevron-left'); });

javascript jquery

Comments

Popular posts from this blog

ruby on rails - Devise Logout Error in RoR -

model view controller - MVC Rails Planning -

php - Working with pretty urls on apache and fastcgi after hhvm installation -