javascript - jQuery removing a previous set class or removing with wildcard -
i have following js sets background class. however, possible remove previous set class?
i cannot remove them contains other classes whatever set on mouseover function or maybe if that's not possible removing wildcard class?
classes being set end *-bean-bags maybe guess not flexible.
whats expected:
- hover item , set class
- hover item , remove previous set class , set new class
anyone have ideas?
$('.ty-menu__submenu-list li').on("mouseover", function () { var menubackground = $(this).attr("data-background"); //console.log(menubackground); $("div.ty-menu__submenu.ty-menu__submenu-to-right").removeclass (function (index, css) { return (css.match (/\b-bean-bags\s+/g) || []).join(' '); }); $("div.ty-menu__submenu.ty-menu__submenu-to-right").addclass(menubackground); });
store class remove in custom attribute, this:
$('.ty-menu__submenu-list li').on("mouseover", function () { //get names of new class var menunewbackground = $(this).attr("data-background"); //get reference element classes. elm = $("div.ty-menu__submenu.ty-menu__submenu-to-right"); //get name of old class var menuoldbackground = elm.attr("data-background-old"); //first remove old, add new, , save name of new, //so can used remove later. elm.removeclass(menuoldbackground) .addclass(menunewbackground) .attr("data-background-old", menunewbackground); });
Comments
Post a Comment