Mostaza Mostaza - 3 years ago 171
Javascript Question

If there is a X class don't add a new Y class

I am a little confused with my JS (I am very new working with JS).

I already have

$('#left-nav-menu').hover(function () {

I would like to achieve if there already is this class "menu-desktop_open_hover" and you click on the DIV with this class "menu-desktop_open_hover" DO NOT add a new class called "menu-desktop_open"

By default when you click the DIV with the class "menu-desktop_open_hover" it is adding a new class called "menu-desktop_open" (we need to keep this class for another things but not when the div is mouse-over/mouse-out)

Do anyone know how could I achieve it?


Answer Source

You can use jQuery hasClass()

$('div').on('click', function() {
  if ( $(this).hasClass("menu-desktop_open_hover") ) {
     //Do something if it's true the class is applied...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download