Skip to content Skip to sidebar Skip to footer

Javascript Change Class Onclick

Im just trying to get to grips with javascript / ajax / jQuery so Im building a ticket system for my website. Anyway, I have a table at the top of the page; ).on('click', 'a', function(e) { $('.subtopic a').removeClass('selected'); // remove the class from all the links $(this).addClass('selected'); // add it to the clicked link }

Note that I am using the new jquery function .on() instead of .click, .live or .delegate. The way .on() works, you will make sure you only bind events to the links in the .subtopic row. Using .on() also guarantees your events will still fire when adding new links dynamically to the DOM.

Solution 2:

You should use :hover pseudo-class.

It will handle mouse over/out for you.

Solution 3:

Check out.toggleClass(), it does what the name suggests.

Post a Comment for "Javascript Change Class Onclick"