Take away the CSS class from an element after reCAPTCHA verification is complete in Next.js

I'm struggling with removing the CSS class btn-disabled from the input button element upon successful verification of a form entry.

I have implemented a function called enableForm to remove the btn-disabled CSS class when reCAPTCHA is verified.

However, despite my function being called (as indicated by the console.log output), I am unable to remove the CSS class which prevents the form from submitting. This used to work fine using regular JavaScript but now I'm facing issues in implementing it on nextjs. How can I successfully remove the CSS class once reCAPTCHA validation is successful?

Below is a snippet of my code:

import ReCAPTCHA from "react-google-recaptcha";

const TopForm = () => {

    const enableForm = function() {
        console.log('yes');
        document.getElementById("btnSubmit").classList.remove("btn-disabled");
    };

    return (
        <div> 
            <input
                id="first_name"
                maxlength="40"
                name="first_name"
                size="20"
                type="text"
                placeholder="First Name*"
                className="field"
            />
            <br />

            <input
                id="last_name"
                maxlength="80"
                name="last_name"
                size="20"
                type="text"
                placeholder="Last Name*"
                className="field"
            />

            <ReCAPTCHA 
                sitekey="XXXXXX"
                onChange={enableForm}
            />

            <input
                id="btnSubmit"
                type="submit"
                name="submit"
                className="btn-disabled btn-alternative width-inherit margin-left-0"
            />
        </div>
    );
}

The CSS properties for the btn-disabled class are as follows:

.btn-disabled {cursor:not-allowed;opacity:0.5;text-decoration:none;pointer-events: none;}

Answer №1

To optimize class management, consider leveraging the state value

import ReCAPTCHA from "react-google-recaptcha";

const TopForm = () => {

const [isActive, setIsActive] = useState(false);


const enableForm = function () {
console.log('yes')
setIsActive(true)
};

return (
 <div> 
 <input
                    id="first_name"
                    maxlength="40"
                    name="first_name"
                    size="20"
                    type="text"
                    placeholder="First Name*"
                    className="field"
                />
                <br />

                <input
                    id="last_name"
                    maxlength="80"
                    name="last_name"
                    size="20"
                    type="text"
                    placeholder="Last Name*"
                    className="field"
                />
          <ReCAPTCHA 
                sitekey="XXXXXX"
                onChange={enableForm}
                />
<input
                    id="btnSubmit"
                    type="submit"
                    name="submit"
                    className={`constant1 ${isActive ? "active" : ""} btn-alternative width-inherit margin-left-0`}
                />

</div>
)

Answer №2

Within your code, there seems to be an oversight where you assigned a function instead of actually calling it.

Here is the corrected version:

const enableForm = function () {
        console.log('yes')
        document.getElementById("btnSubmit").classList.remove("btn-disabled");
    };

It should now function as intended.

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

The functionality to save user likes in React is not properly functioning on the like button

I created a like button in React that saves my choices, but it seems to be not saving the choices of other users. Additionally, it doesn't appear to restrict only authenticated users from marking likes. Can someone please help me identify what I' ...

React app experiencing CORS problem while trying to access external API

When attempting to utilize fetch with basic authentication and an external API, I encountered the error shown in the screenshot: https://i.stack.imgur.com/sKkhh.png Using Fetch method const fetchBusVendors = async () => { const url = 'htt ...

When attempting to debug JavaScript in Edge with Visual Studio Code, an error message stating 'Failed to load source map for chrome-error...' was encountered

Attempting to troubleshoot JavaScript code in Visual Studio Code is resulting in an error: Could not read source map for chrome-error://chromewebdata/: Unexpected 503 response from chrome-error://chromewebdata/edge-elixir-neterror.rollup.js.map: Unsupporte ...

Switching button appearance when hovered over

Looking to create a page layout with 4 buttons, each occupying one quadrant of the page? These buttons should feature images as their background and change to different images when hovered over. Wondering how this can be achieved using CSS? ...

Is it possible to reposition a label in Material-UI?

Currently, I am utilizing Material UI for react and I am looking to shift the position of the label within text fields to the right. However, due to its motion, it does not appear as visually appealing. Is there a way to modify this while maintaining the ...

Retrieve the structure from a React application

When it comes to documenting architecture, the process can be incredibly beneficial but also quite time-consuming and prone to becoming outdated quickly. I have come across tools like Doxygen that are able to extract architectural details such as dependen ...

Troubleshooting Issue: Unable to Collapse Bootstrap Responsive Navbar Button with Custom CSS Modifications

I recently created a responsive HTML page using Bootstrap, then converted it to WordPress and made some custom CSS adjustments. In the original HTML version of the page, the navbar collapse button worked perfectly when the screen was resized. However, afte ...

Learn How to Implement Styling in Material UI using Styled Components

Utilizing styled-components with MaterialUI integration. Encountering the following error in LoginTextField component and seeking a resolution. Error Message: [ts] Type '{ width: number; }' is missing the following properties from type &apos ...

Encountering an error stating "target" property is undefined while testing the useState function

I'm attempting to utilize these state methods when passing state from a parent component to a child component const [bio, setBio] = useState(""); const [gravatar, setGravatar] = useState(""); However, I am encountering the following error: ✓ Shou ...

Responsive full-screen background image display

Why is the background image not appearing as a full image on large screens but fitting fine on small screens? <!DOCTYPE html> <html dir="ltr"> <head> <meta charset="utf-8" dir="ltr" /> <title ...

Issue with React occurring when attempting to delete an input component

I seem to be facing a challenge that I can't quite figure out whether it's related to React or not. To help illustrate the issue, I've created a simple example outside of my project: https://codepen.io/as3script/pen/VMbNdz?editors=1111 Wit ...

Switch the scroll direction in the middle of the page and then switch it back

Yesterday, while browsing online, I stumbled upon this website and was amazed by the unique scroll direction change from vertical to horizontal mid-page. I'm curious about how they managed to achieve that effect. Does anyone have insight into the pro ...

How can you store JavaScript objects to files, including their methods, in a Node.js environment?

In reading about saving objects to files in Node.js here on Stack Overflow, I understand the process. However, I am curious if there is a method that allows for writing an object in a manner that would enable me to reload the object into memory along wit ...

What is the best way to change the value of a key in a JSON Object?

I am currently utilizing _underscore library. My goal is to change the value of a specific key. var users = [{ "_id": { "$oid":"3426" }, "name":"peeter" }, { "_id": { "$oid":"5a027" }, "name":"ken" }, { "_id": { "$oid":"5999" }, ...

"Is it possible to draw on top of a canvas element that's already been

I'm currently working with an OpenLayers map that includes WMS layers with time steps. My goal is to create a loop that updates the time for each step and then saves the image once it's rendered. I've been referencing the example code from t ...

CSS deep level selectors

For the following HTML/CSS code, I am trying to target only level 1 "row" elements and not their nested counterparts. However, when using the child selector, it still selects the nested elements. How can I achieve selecting only level 1 "row" elements? ...

Associating specific information with individual elements utilizing HTML text box

Seeking guidance on implementing annotations feature for a scatter-plot using d3.js v3. My goal is to show a text-box upon clicking each data point, then display that entered text as a tool-tip specific to that data point. Here's how I'm approach ...

Applying CSS styles to a page depending on certain conditions

Currently, I have a page in SharePoint that can function as a pop-up. I am using JavaScript to identify whether it is a pop-up by checking if window.location.search.match("[?&]IsDlg=1"). However, I am struggling to figure out how to insert CSS based on ...

CSS media query specifically for mobile devices regardless of screen width

I've been trying to use a dragscroll plugin to scroll through content, but I've run into an issue where it doesn't work on mobile devices. By mobile devices, I mean those without a mouse cursor, like smartphones and tablets with touchscreen ...

Retrieve the image by its unique identifier while viewing a preview of the image before it is uploaded

Below is the script I am using to preview an image before it is uploaded. The HTML structure looks like this: <div> <img id="image" src="#"> </div> <input type="file" accept="image/gif, image/jpeg, image/png" onchange="readURL(th ...