The removeClass method in Jquery seems to be ineffective when attempting to create a simulated active button using a DIV element

Since CSS is not my strong suit, I'm attempting to create a pressed button effect using the :active pseudo-class by applying a "glow" class to a DIV element with jQuery's addClass method and then removing it after a brief delay.

This is how I have written the code:

  $( ".cpUpbtnsclass" ).on('click', function() {

        console.log($( this ).attr('id')+" is clicked and a class will be added ! ");

           $( this ).addClass( "glow");

        setTimeout(function(){
            $( this ).removeClass( "glow");
        },300);
    });

And here is the CSS class that I am trying to add:

.glow {
box-shadow: 0px 0px 15px 5px rgba(255, 255, 190, .75);
}

I can see the glow effect being added with addClass, but it doesn't seem to disappear after 300ms as intended.

Answer №1

In the scenario of a timeout, a separate context is established where the reference to the element changes:

$( ".cpUpbtnsclass" ).on('click', function() {
    var self = this;

    $( this ).addClass( "highlight");

    setTimeout(function(){
        $( self ).removeClass( "highlight");
    },300);
});

Answer №2

Attempt

$(".cpUpbtnsclass").on('click', function () {
    var current = $(this); //store selected element 
    current.addClass("glow"); 
    setTimeout(function () {
        current.removeClass("glow"); //use stored element 
    }, 300);
});


The use of timeout creates a separate scope, causing $(this) to refer to a different element within the setTimeout function.

Answer №3

$(document).ready(function(){
    $(document).on('click', '.upBtnClass', function(){
        console.log($( this ).attr('id')+" has been clicked and a special effect will be applied! ");
           $('.upBtnClass').addClass( "shine");
        setTimeout(function(){
            $('.upBtnClass').removeClass( "shine");
        },300);
    })
})

Check out the live demonstration http://jsfiddle.net/8pc9m/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to utilize jQuery to insert a chosen form into a table?

Here is the code example I am working with: <label for="">Select Ticker: </label> <select class="form-control" style="display: inline" id='selected'> {% for p in tickers %} <option value="{{ p.tick ...

CSS Priority - determining the ultimate winner

I have an upcoming exam and I'm reviewing the previous exam paper that was given to me. The question is: When multiple style sheet rules are applied to the same element, which type of rule takes precedence? a. Any declaration from the browser b. ...

Styling limitations encountered when using lang="scss" with scoped css

My current project involves using Quasar and I am attempting to style the first column of q-table: <style lang="scss" scoped> td:first-child { background-color: #747480 !important; } </style> Unfortunately, this code does not seem to have a ...

What is the best way to bridge the distance between a shrunken iframe and the following element?

https://i.sstatic.net/AZ7T4.png My iframe was resized to 65%, but the containing "div" still occupies the original iframe size, creating a large gap between the iframe and the next element. I'm not sure how to remove this gap. I want the next element ...

What is the best way to eliminate the space underneath a graph?

Despite trying multiple solutions, I'm still struggling to remove the excess blue space below the graph that appears when clicking the submit button. Any insights into what might be causing this issue? JSFIDDLE Below are the CSS styles related to t ...

Personalized Owl Carousel - OWL.JS Hover Pause Feature

I have been working on a slider in Codepen that is functioning well, and now I am trying to tweak it for my website. My goal is to make the carousel pause when someone hovers over it. I've experimented with all the options available on the owl slider ...

What is the best way to ensure that all background images are displayed in IE when printing?

I am currently working on formatting a page for printing in Internet Explorer. I have encountered an issue where when I select the "Print background and images" option in Page Setup and preview the page, only certain icons are displaying. However, all icon ...

Does text disappear when hovered over in CSS?

Having some trouble getting a text to display on hover over the parent container while adjusting the opacity. Despite trying multiple methods, it just won't show up no matter what I do! Here is my current code: .project{ position: relative; ...

Looking to rearrange the layout of jqgrid

I need to reposition the jqgrid away from its default top left position. I have attempted some adjustments but they are not working as expected. Below is the code snippet for your review and suggestions: Here is the HTML code snippet: <div> &l ...

Easy steps to transform a basic table into a DataTables plugin

var jsonData = '{"data":{"2G":[{"amount":"9","detail":"35 MB 2G Data , Post 35 MB you will be charged at 4p\/10kb","validity":"1 Day","talktime":"0"},{"amount":"16","detail":"90 MB 2G Data, Post 90 MB you will be charged at 4p\/10kb","validi ...

Jquery Autocomplete with Dynamic Text Updates

I am having an issue with the autocomplete feature on my website. Whenever I enter a search and select an option, the text in the box changes to the selected value instead of keeping what I originally entered. How can I prevent this from happening? <sc ...

Safari displays text in a noticeably bigger font size compared to other browsers

I'm currently working on a website and have noticed that the fonts appear larger on Mac's Safari compared to other browsers. We are using the 'Open Sans' font from Google Fonts for our website. For example, here is a CSS snippet for ti ...

Is it possible to utilize an AJAX request to access a custom JSON file stored locally in order to dynamically generate Bootstrap

I want to create a page featuring Bootstrap 4 cards with an image, title, brief text, and a "read more" button (basically a blog page with a list of posts). The catch is that this all needs to be done without a server, completely locally. Since this is a ...

The x-axis values in Amchart are transitioning rather than shifting

I'm facing an issue with my x-axis values as I'm using real-time amcharts. The x-axis values change every 3 seconds, but instead of smoothly moving, they abruptly change to the next value. I would like it to slide smoothly like this example: htt ...

How can I create multiple divs that look alike?

I've taken on the challenge of developing our own interpretation of Conway's "Game of Life" for a project. To represent my 20x20 grid, I decided to create nested divs - the whole grid is one div, each row is a div, and every cell within that is a ...

How to access a variable from within a Phonegap DB function and return it outside of the

I'm struggling with a phonegap db JavaScript script that isn't returning the final string variable "feeds" outside the function. It's currently showing as "undefined". I need help making the necessary changes to properly return the "feeds" v ...

What is the HTML code needed to stack two divs on top of each other?

Many individuals seem to be inquiring about this issue, yet I am still unable to resolve it completely. The image I have linked closely resembles what I am aiming for. I would like the image in each section to flow from left to right, as it currently does, ...

The CSS file is not appearing, causing the styling not to be applied

I've included the following line in my html file: <link rel="stylesheet" href="css/styles.css"> Despite having the css file linked, the styling is not visible on the page. The HTML remains plain. The structure of my files is as follows: file ...

Adjust the background to scroll to the bottom of the image first and then change it to be fixed

Here is the code I have written: body{ margin:0; padding:0; line-height: 1.5em; background-image: url(http://fehlbelichtet.stefanwensing.de/wp-content/uploads/sites/6/2016/04/alte-strasse-endlos.jpg); background-repeat:no-repeat; background-attachment: ...

Leveraging the Power of jsPDF in Your Google Apps Scripts

Is it feasible to integrate jsPDF into a Google Apps Script project? I am attempting to create documents using templated HTML, but certain CSS styles like background-color do not translate to PDF when using the getAs() function. Given the limitations of ...