The newly added highlighted CSS in jQuery cannot be removed once an alert is generated

http://jsfiddle.net/Cyjye/ I am a beginner with jquery. I have created an HTML table shown in the jsfiddle link. Initially, the selection should be made from the second row, second cell, but in my HTML table, the selection is being made from the first row, second cell. I only have 30 minutes to spare for this issue. If a user selects more than 30 minutes, an alert saying "Time slot not more than 30 minutes" should pop up. The alert works correctly, however, the added CSS remains even after the alert is dismissed. Whenever I release the mouse button after the alert, the cell is highlighted with CSS, which I don't want. My goal is to remove the recently added CSS when the alert generates and prevent any cells from being highlighted with CSS upon releasing the mouse button. Despite my efforts, I haven't been able to find a solution. When I highlight a 30-minute time slot and click on the "select patient" button (found below the HTML table), the highlight CSS should be removed.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Drag selection example</title>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">

        google.load("jquery", "1.3.2");

        google.setOnLoadCallback(function ()
        {
            var active = false;
            var lastActionWasError = false;
            $('#tableAppointment tr td:nth-child(2), #tableAppointment tr td:nth-child(3)').mousedown(function (ev)
          {
                active = true;
                $(".csstdhighlight").removeClass("csstdhighlight"); // clear previous selection
                ev.preventDefault(); // this prevents text selection from happening
                $(".csstdhighlight").removeClass("csstdhighlight");
                $(".temp_selected").removeClass("temp_selected");
                $(this).addClass("csstdhighlight");
                $(this).addClass("temp_selected");
            });

            $('#tableAppointment tr td:nth-child(2), #tableAppointment tr td:nth-child(3)').mousemove(function (ev)
            {
                if (lastActionWasError)
                {
                    $(".csstdhighlight").removeClass("csstdhighlight");
                    $(".temp_selected").removeClass("temp_selected");
                    lastActionWasError = false;
                }
                if (active)
                {
                    $(this).addClass("csstdhighlight");
                    $(this).addClass("temp_selected");
                }
                if ($('.temp_selected').length > 6)
                {
                    alert("Time slot not more than 45 minutes.");
                    $(this).removeClass("csstdhighlight");
                    $(this).removeClass("temp_selected");
                    lastActionWasError = true;
                    return false;
                }
            });
        });
    </script>
    <style type="text/css">
        .csstdhighlight
        {
            background-color: #ccffcc;
        }
    </style>
</head>
<body>
    <table id="tableAppointment" cellspacing="1" width="50%" border="1" align="center">
        <tr>
            <td bgcolor="#ffffff">
            </td>
            <td class="csstablelisttd">
            </td>
            <td class="csstablelisttd">
                <b>Patient Name</b>
            </td>
        </tr>
        <tr>
            <td class="csstablelisttd" valign="top" width="70px">
                8:00AM
            </td>
            <td class="csstablelisttd">
                0
            </td>
            <td class="csstablelisttd">
            </td>
        </tr>
        <tr>
            <td class="csstablelisttd">
            </td>
            <td class="csstablelisttd">
                15
            </td>
            <td class="csstablelisttd">
            </td>
        </tr>
        <tr>
            <td class="csstablelisttd">
            </td>
            <td class="csstablelisttd">
                30
            </td>
            <td class="csstablelisttd">
            </td>
        </tr>
        <tr>
            <td class="csstablelisttd">
            </td>
            <td class="csstablelisttd">
                45
            </td>
            <td class="csstablelisttd">
            </td>
        </tr>
        <tr>
            <td class="csstablelisttd" valign="top" width="90px">
                9:00AM
            </td>
            <td class="csstablelisttd">
                0
            </td>
            <td class="csstablelisttd">
            </td>
        </tr>
        <tr>
            <td class="csstablelisttd">
            </td>
            <td class="csstablelisttd">
                15
            </td>
            <td class="csstablelisttd">
            </td>
        </tr>
        <tr>
            <td class="csstablelisttd">
            </td>
            <td class="csstablelisttd">
                30
            </td>
            <td class="csstablelisttd">
            </td>
        </tr>
        <tr>
            <td class="csstablelisttd">
            </td>
            <td class="csstablelisttd">
                45
            </td>
            <td class="csstablelisttd">
            </td>
        </tr>
    </table>
</body>
</html>

Answer №1

If you want to get rid of the highlighted CSS in the previously selected cell, you can try this:

$(this).removeClass("csstdhighlight");
$(this).removeClass("temp_selected");

Check out the JSFiddle example here

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

Text box size in user input not adapting on mobile devices

Website: Visit the Cutter Travel Calculator here I've encountered an issue with my user input text boxes on mobile. Despite setting their width using the formidable forms plugin, the size adjustment doesn't seem to apply when viewed on mobile de ...

Is there a way to manipulate the src value using jQuery or JavaScript?

var fileref = document.createElement('script'); fileref.setAttribute("type","text/javascript"); fileref.setAttribute("src", "http://search.twitter.com/search.json? q="+buildString+"&callback=TweetTick&rpp=50"); ...

Implementing a redirection of Dojo Data grid cell values to a different destination

I am working with a Dojo data grid that contains 10 rows (cells) of values. When the first row is clicked, I need it to redirect to another HTML page and carry along the values from the first row. Can you assist me in achieving this functionality? ...

What is the most effective way to extract information from a .txt file and showcase a random line of it using HTML?

As a newbie in HTML, my knowledge is limited to finding a solution in C#. I am attempting to extract each line from a .txt file so that I can randomly display them when a button is clicked. Instead of using a typical submit button, I plan to create a custo ...

Featuring my Cookie page prominently on my website's homepage

I am facing a simple issue: I have a cookie verification page (cookies_check.php) that I want to include on my home page (accueil.php). So, currently, the only code on accueil.php is: <?php include ('Controleur/cookies_check.php'); ?> He ...

In Safari, my layout is disrupted by the mere mention of the word 'City'

There's something strange happening in Safari that I can't quite figure out. Here's how my layout looks in Chrome: https://i.sstatic.net/lPhnP.png But when viewed in Safari, it looks like this: https://i.sstatic.net/IMnQb.png For some r ...

What is special about this element, textarea?

Hey there, I've got this JavaScript code that currently works on all textarea elements on the site. However, I'd like it to only work on one specific element. function limits(obj, limit) { var text = $(obj).val(); var str_length = $(obj) ...

Illustrating a fresh DOM element with intro.js

I am currently utilizing intro.js to create a virtual 'tour' for my website. As I aim to provide a highly interactive experience for users, I have enabled the ability for them to engage with highlighted DOM elements at different points in the tou ...

The never-ending scroll feature in Vue.js

For the component of cards in my project, I am trying to implement infinite scrolling with 3 cards per row. Upon reaching the end of the page, I intend to make an API call for the next page and display the subsequent set of cards. Below is my implementatio ...

Guide on disabling a hyperlink in a table column under specific conditions using Angular2

I am working with a table that has 4 columns: Name, id, Dept, City. The row data and column data are provided as an array from a typescript file and I am iterating through *ngFor to display the content. Below is a snippet of my code: <tbody> < ...

JavaScript issue with confirm/redirect feature not functioning as expected

A demonstration of JavaScript is utilized for deleting an employee in this scenario... <script type="text/javascript"> function deleteEmployee(employee) { var confirmation = confirm('Are you sure?'); if(confirmation) { ...

Add() function is not duplicating the formatting

I'm attempting to replicate the content below inside a DIV. <ul class="pie-legend"><li><span style="background-color:#0066CC"></span>10-0-1</li><li><span style="background-color:#33CC33&q ...

Ways to track down an ajax error response?

Whenever I encounter an AJAX error response with jQuery, the following log is displayed: jquery.js:8630 OPTIONS http://10.0.1.108:8000/api/v1.0/auth/ net::ERR_CONNECTION_REFUSED Object {readyState: 0, status: 0, statusText: "error"} Within my object, the ...

When the page is refreshed, the value bounces back and forth between

I've encountered a strange issue on my page with 6 textboxes. Whenever I enter values into these text boxes and then refresh the page, the values seem to jump to the next textbox (the one after the next). For example, if I enter values as follows: [ ...

Get an MP4 on your iPhone (iOS) by downloading it from a link on a webpage

Despite trying various methods such as the HTML5 download tag, JavaScript, JQuery, and other techniques, I have not been able to successfully download a file to the Album. While photos can be saved by long clicking and selecting "Save Image", videos do n ...

The CSS class is not functioning properly while the ID is working perfectly

A question I have pertains to the navigation menu on my website and its default styling... <nav> <ul id="MenuBar1" class="MenuBarHorizontal"> <li> <a href="#">Home</a> </li> <li> <a class="Menu ...

Change the order of the div to be placed before the previous element

In this scenario, the third div is positioned to the right of the second div rather than the first div. There is space available to the right of the first div, but it is constrained from appearing above the second div. How can I adjust the placement of th ...

How can I show the localStorage value in an HTML5 template using Angular2?

I have two keys stored in localStorage and I want to display one of them on my template. I'm having trouble accessing these values. I even created an interface specifically for storing the value of the currentUser key from localStorage. How should I g ...

disable input box stationary

Is there a way to make a checkbox stay disabled even after refreshing the page? Currently, when I click the checkbox it disables, but upon refreshing the page, it goes back to being enabled. $( "input[type='checkbox']").click(function(event){ ...

Is it possible for certain text within a textbox to activate a jQuery event?

In the development of my MVC ASP.Net application, there is a requirement for users to fill out a form. Specifically, there is a text box where users must input the duration of their current employment. If the user indicates less than three years, an additi ...