Removing the css class when an element loses focus

In order to improve user experience on my web page, I have implemented a validation check on a form before submitting it back to the server. The validation process is successful and applies a custom CSS class called ErrorControl to input fields when necessary.

My goal is to provide immediate feedback to users when they correct any errors in their inputs. To achieve this, I am utilizing the .focusout function in JavaScript.

Below is the structure of my form:

<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title">Donations</h3>
    </div>
    <div class="panel-body">
        <asp:ValidationSummary runat="server" HeaderText="There were errors on the page: " />
        /* Form controls go here */
    </div>
    <div class="panel-footer">
        <asp:Button runat="server" CssClass="btn btn-success pull-right" OnClick="SubmitButton_Click" ID="SubmitButton" Text="Submit Form" />
    </div>
</div>

Although the validation process works well, I now want to remove the ErrorControl class immediately after an input field loses focus. I have written some JavaScript code for this purpose, but it's not functioning as expected.

Here is the snippet of the problematic JavaScript:

/* Problematic JavaScript */
$(".panel-body").focusout(function (e) {
    var id = e.target.id;
    if (id == "firstname" || id == "lastname") {
        alert("First if hit");
        if (e.target.value != "") {
            alert("second if hit");
            $(id).removeClass("ErrorControl");
        }
    }
});

I suspect there is only one incorrect line of code causing the issue. If needed, I can share the form on JSFiddle, keeping in mind that the asp:requiredValidator may not function properly.

Answer №1

When using the removeClass function on $(id), make sure to include the "#" symbol before the ID name, like this: $("#firstname"). This indicates that you are selecting an element by its ID in JQuery.

Here's a revised version for better functionality:

$("#"+id).removeClass("ErrorControl");

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

Display a DIV element when a specific date is chosen using bootstrap-datepicker

Looking to incorporate the bootstrap-datepicker plugin using the provided markup... Is there a way to display a DIV element when selecting a specific date and then hide it again when another date is chosen? <head> <script src="https://cdnjs.cl ...

Inject an HTML or jade webpage into a div within another HTML or jade webpage

I'm facing an issue in my Node.js project with a script (JS) that is responsible for loading a Jade page into a div of another Jade page using $("#div").load(directory). Despite specifying the directory of the jade page to be loaded, I keep getting an ...

Unchanging bottom section with changeable information

I have a unique website design in mind that involves using an image as the background and placing the content within a box. View my website design here However, I am facing an issue with a specific page on this website. The page contains accordion buttons ...

Running a Python Selenium script

I need some help executing this script using selenium. <div class="vbseo_liked"> <a href="http://www.jamiiforums.com/member.php?u=8355" rel="nofollow">Nyaralego</a> , <a href="http://www.jamiiforums.com/member.php?u=8870" rel="nofollo ...

What is the process of utilizing a <li> for a hover selector in jquery?

I've encountered an issue with a list that I am trying to modify its content when hovering over them. Despite using the id as a selector for triggering the hover function, all list items change color instead of just the intended one. The challenge lie ...

clicking on a link with the symbol "#" in the href attribute in Firefox prevents the setter

Good morning everyone, It's early Monday morning and I'm having trouble understanding why this particular line works in Internet Explorer but not in Firefox. <a class="button" href="#" onclick="setMaintenanceMode(false);">disable</a> ...

Utilize Function to Gain Access to React Context

As I work on developing a package that enhances the responsiveness of my React widget, I have encountered an issue. The responsiveness now relies not on the viewport width, but rather on the width of the widget container element. Presently, I am wrapping ...

datetime-local format changing after selection

In an ionic application running on an android system, there is a issue with the formatting of <input type='datetime-local'> inputs. After a user selects a date, the Start input is formatted differently than the Ende input. Attempts to use t ...

The popover box adjusts its width to fit the contents in Chrome, however, it does not do so in

Having issues with the container width in Edge compared to Chrome despite the Sass code working fine in Chrome. Here's the code: .popover { font-family: 'Tahoma', sans-serif; max-width: 100%; box-shadow: 0 0 15px 6px #646262; ...

Improving the Performance of HTML/CSS Animations - Enhanced Animation Speed

I have created an HTML page with CSS animation and transitions. <div class="container-fluid" id="team"> <div class="row first"> <div class="wrapper"></div> </div> <div class="row second"> <div class="wrapper" ...

What makes a function in JavaScript special as both a constructor and an object?

After conducting extensive research on this topic, I have not been able to find a definitive answer. According to one source, when the JavaScript engine encounters a function statement, it creates a new Function() object, leading me to believe that it coul ...

The React component is experiencing a delay in its updates

I've been experiencing delayed updates when using React.useEffect(). Can anyone shed some light on why this might be happening? function Process(props) { const [results, setResults] = React.useState({ number: "", f: {} }); let ...

Tips for placing an object within the boundaries of a canvas using three.js

At the moment, I am utilizing the threejs library to design 3D objects. However, I am facing an issue where the object is overflowing outside the canvas if it is too long. You can view my code snippet on JSFiddle. Script import * as THREE from 'https ...

The Google calendar is displaying an invalid JSON file

We're in the process of developing a website using AngularJS and we need to integrate Google Calendar events. I've attempted to retrieve the data from a Google Calendar. You can locate the JSON file here: http://www.google.com/calendar/feeds/[em ...

AngularJS is not showing the dropdown options as expected

I am facing an issue where the dropdown list options are not displaying, even though I am able to fetch the data in the controller but not in HTML. Here is the code snippet: In HTML <select name="nameSelect" id="nameSelect" ng-model="model.name"> ...

Search the database for two fields within passport configuration

I'm facing an issue with validating both the username and email fields on signup before adding a user to the database. Most examples for passport only cover checking one field, such as the username, but I need to figure out how to validate two fields ...

Angular 13: Problems arise with custom theme in Angular Material version 13

I've set up a custom theme palette for my project that works perfectly with version ^12.2.13 of Angular Material, but not with ^13.2.3. Here's the SCSS code for my custom theming: my-custom-theme.scss @import '~@angular/material/theming&apo ...

Can you show me how to condense this using the ternary operator?

Although I don't necessarily have to refactor this code, I am intrigued by the idea of doing so. handleError: ({ error, email, password }, props) => authError => { if (email === "" || password === "") { return { error: `Plea ...

Mastering Fluent UI Web: Adjusting the border radius of a TextField component

I am in the process of creating a custom user input control that includes a TextField, a Dropdown, and a Button. To ensure that the TextField and Dropdown appear as a cohesive unit rather than two separate elements, I attempted to use two specific styles ...

Definition list with alternating left and right alignments

Here is a definition list with a specific styling: dl { margin: 0; } dl.interview dt { color: #A8A67A; } dl.interview dd { margin-left: 0; } .interview dt:before { content: 'Q. '; } .interview dd:before { content: 'A. ...