Removing class name on input focus

I am currently developing a complex multi-form using bootstrap. Within my validateForm function, I have incorporated logic to add an "Invalid" class if the input value is empty, indicating that the user has not entered any information.

When the user clicks on the next button, the input field will display a reddish background color, signifying that it requires input.

My question is, how can I remove the "Invalid" class in two scenarios:

  • When the user starts typing in the input field

OR

  • When the user clicks or focuses on the input field

This action should reset the background color back to its original state.

Here is the JavaScript code snippet:

function validateForm() {
var x, y, i, valid = true;
x = document.getElementsByClassName("tab");
y = x[currentTab].getElementsByTagName("input");
for (i = 0; i < y.length; i++) {
    if (y[i].value == "") {
        y[i].className += " invalid";
        valid = false;
    }
}
if (valid) { document.getElementsByClassName("step")[currentTab].className += " finish"; }
return valid;
}

And here is the relevant CSS code:

input.invalid {
    background-color: #ffdddd;
}

Your help and guidance are highly appreciated!

Answer №1

Here is a similar example:

const allInputs = document.querySelectorAll("input");
allInputs.forEach(function(input) {
    input.addEventListener("focus", function(event) {
        input.classList.remove("error");
    });
    input.addEventListener("input", function(event) {
        input.classList.remove("error");
    });
});

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

Multiple jQuery load() requests are being triggered by calling it from within the ajaxComplete() callback

I've been stuck on this issue for quite some time now. To troubleshoot, I suggest using Firebug to inspect the AJAX requests. It seems that they are multiplying with each click of the next and previous buttons, causing the page to load slowly: You ca ...

Using jQuery to slide in dynamically generated content

I am in the process of developing a straightforward content slider that has the capability to manage dynamically added content. Unfortunately, none of the supposedly "lightweight" plugins I came across offered this functionality or, if they did, it was not ...

Exploring the Possibilities of Wordpress Search with Multiple Dropdown Options

Is it possible to search across multiple categories? For example, I have 4 dropdown menus: 1. City 2. Area 3. Month 4. Products/Services When a user visits my site, they will see a static page with 4 dropdown lists and a "search" button. After the user ...

Swapping the icon in the panel heading of Bootstrap 3 Collapse based on its collapse status

Despite many attempts, I haven't been able to make any of the provided answers work in my code. My setup involves using Bootstrap 3 panels and collapse functionality to display advertisements. In the default view, only the advertisement heading is vi ...

Struggling to implement nested routes with react-router-dom version 5.2.0?

I'm currently working on implementing nested routing in React using react-router-dom 5.2.0. For a better understanding of the project, you can access the CodeSandbox link here: https://codesandbox.io/s/nested-routes-8c7wq?file=/src/App.js Let's ...

Dawn Break Alarm Timepiece - Online Platform

My buddy recently purchased a "sunrise alarm clock" that gradually brightens to mimic a sunrise and make waking up easier. I had the thought of replicating this effect with my laptop, but I've been facing issues with getting the JavaScript time funct ...

Refreshing the package using Bower

I'm facing an issue while trying to upgrade angular from version 1.0.5 to 1.0.6 using Yeoman. Despite clearing the cache and checking the Github repository, it still installs version 1.0.5. Is there a workaround to force the update to version 1.0.6? ...

My child template in Angular UI is not loading properly due to the presence of multiple views when the URL changes

After reviewing a couple of questions similar to this on various forums, I may have overlooked a crucial detail in them. Upon loading http://localhost:8000/characters/#/mages/detail/3, I am being redirected to my 'otherwise' URL: $urlRouterProvi ...

Which regular expression can match both the start and finish of a string?

I need help with editing a DateTime string in my TypeScript file. The specific string I'm working with is 02T13:18:43.000Z. My goal is to remove the first three characters, including the letter T at the beginning of the string, as well as the last 5 c ...

I am curious if there exists a VSCode plugin or IDE that has the ability to show the dependencies of TypeScript functions or provide a visual representation

Are there any VSCode plugins or IDEs available that can display the dependency of TypeScript functions or show a call stack view? I am looking for a way to visualize the call stack view of TypeScript functions. Is there a tool that can help with this? Fo ...

updating the content of a webpage using innerHTML on Squarespace platform

Could anyone help me with changing the status from "sold out" to "coming soon" on this specific page? Below is the code I have tried, but unfortunately, it is not working as expected. window.onload = function() { document.getElementsByClassName("produ ...

What is the reason for receiving an undefined value when calling getInitial

In my index page like this: function Index({posts}) { console.log(posts); return ( <div> Homepage {/* {posts && posts.length > 0 && posts.map(post => <h1 key={post._id}>{ post.title} ...

anchor text substitution for hyperlink

I currently have a code snippet that replaces a span class with a hyperlink. The hyperlink contains an abbreviation, and the alternate text for the link also includes the same abbreviation. Now, I am looking to replace the second abbreviation in the alte ...

Please make another request in 1 minute using an observable

I am looking to continuously load data from 5 API's every minute in order to refresh the page. My approach involves using Observable.forkJoin to gather all the data by sending requests simultaneously and then placing it inside a setInterval function. ...

Verifying the visibility of an element

I am facing a challenge with a list of apps displayed on a non-angular page. The availability of these apps depends on the subscription level purchased by the user. Even if an app has not been purchased, it is still listed but displayed with an overlay (pl ...

Issues with AngularJS email validation specifically related to domain names are causing errors

Hello, I'm currently working on an AngularJS application where I'm implementing email validation. I'm facing an issue where I expect an error message to appear when I enter 'test@test', but it's not working as intended. Here ...

What is the best way to perform a deep sumBy operation with lodash?

var data= [{ "item_name": "Jumbo Combo", "item_key": "9lazhy15tp2fgo72", "item_price": 25, "quantity": 1, "ingredients": [{ "group_key": "fg1udvnz81qwpxtp", "key": "kcck54k7h3fzp5f0", ...

Is a Selenium loop a viable option?

</head> <body> <table cellpadding="1" cellspacing="1" border="1"> <thead> <tr><td rowspan="1" colspan="3">test</td></tr> </thead><tbody> <tr> <td>click& ...

What is the best way to emphasize a link to the current page in vue.js?

I am working on a sidebar feature that displays several dynamically generated links (similar to creating notes and then linking to them): https://i.stack.imgur.com/1lxcy.png Users can add a new note by clicking the 'Add stage' button. The list ...

Does element.click() in Protractor's Webdriver method return a promise, or is there a way for it to handle errors?

Is the element(by.css()).click() method returning a promise, or is there a way to catch and assert against any errors that may occur? In my scenario, I have a component that is not clickable, and I want to handle the error when this happens. I also want t ...