Search code examples
jqueryaddclassremoveclassseconds

Add and remove class Jquery on click


Is there any way to add a class on click and then remove it after 2 or 3 seconds? Thanks in advance


Solution

  • Try this:

    $("#your_click_element_id").on('click', function()
    {
        $(this).addClass("class_name");//or $("#id_of_whatever_you_want)
        setTimeout(function() {
            $(this).removeClass('class_name');
        }, 2000);
    });
    

    setTimout will wait for 2000 ms and then execute the .removeClass().

    <style>
    .class_name{
        ...
    }
    </style>