The console is not displaying the data from the useForm

I am working on a project to create a Gmail clone. I have implemented the useForm library for data validation. However, when I input data into the form and try to print it to the console, nothing is being displayed.


const onSubmit = (data) => {
        console.log(data);
    };


<form onSubmit={handleSubmit(onSubmit)}>
            <input name='to' placeholder='To' type='text' {...register('to', { required: true })} />
            <input name='subject' placeholder='Subject' type='text' {...register('subject', { required: true })} />
            <input name='message' placeholder='Type your message here...' type='text' className='sendMail_message' {...register('message', { required: true })} />

            <div className='sendMail_options'>
                <Button className='sendMail_send'>Send</Button>
            </div>
        </form>

Answer №1

Include the prop type='submit' in the button component.

<Button type='submit' className='sendMail_send'>Send</Button>

Answer №2

<script type="text/javascript">
    function fetchFormData(formId){
        let formData = [];
        let formDetails = document.getElementById(formId)// retrieve the form
        for ( let i = 0; i < formDetails.elements.length; i++ ) {
            let element = formDetails.elements[i];
            if(element.name !== '') {
                formData[encodeURIComponent(element.name)] = encodeURIComponent(element.value);
            }
        }
        return formData;
    }
    const handleFormSubmit = () => {
        let dataReceived = fetchFormData('formId');
        console.log(dataReceived)
        //task: validate data before submission
        return false;
    };
</script>

<form onsubmit="return handleFormSubmit()" id="formId" >...</form>

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 ReactJS for animating elements based on the user's scrolling behavior?

Is it possible to create a website similar to Apple's Airpod Pro using React? And what about the performance aspect? ...

Determine the size of a BSON object before saving it to a MongoDB database using

I am currently in the process of determining the best way to calculate the size before storing certain data in MongoDB. After writing a script that parses and combines data into a single document, I have encountered an error when trying to use instance.sav ...

Learn the process of automatically copying SMS message codes to input fields in Angular17

After receiving the first answer, I made some changes to the code. However, when testing it with Angular, I encountered several errors. How can I resolve these issues? TS: async otpRequest() { if ('OTPCredential' in window) { const ab ...

What methods can be used to construct components using smaller foundational components as a base?

Is there a more elegant approach to constructing larger components from smaller base components that encompass common functionalities, akin to an interface in the OOP realm? I'm experimenting with this concept, but it feels somewhat inelegant. Repor ...

Can you provide me with a comprehensive list of all the colors available in Twitter Bootstrap 3?

I've integrated the twitter bootstrap sass 3.0.3.0 gem, which is supposed to be the latest version of twitter bootstrap 3.0.3. I'm trying to customize the colors using the 'customize' section of the LESS variables, but I can't seem ...

Model Abstracted Aurelia Validation

I recently completed an online course on Aurelia by Scott Allen where he introduced the Aurelia-Validation plugin for form validation. As a result, my view-model now looks like this in edit.js: import {inject} from "aurelia-framework"; import {MovieServi ...

Is there a way to have content update automatically?

After writing this block of code, I discovered that clicking on one of the circles activates it and displays the corresponding content. Now, I am looking for a way to automate this process so that every 5 seconds, a new circle gets activated along with its ...

Color overlay on top of image background

I'm struggling to create a striped colored background with a narrow center single color background on top for showcasing articles and photos. However, I am unable to get the colored background to display correctly. Despite trying different approaches ...

Creating a seamless integration of Material UI V5 ThemeProvider in Storybook V6 within the NX workspace involves a series

I am currently utilizing nx.dev as my monorepo setup with various React clients. With NX, I have centralized the configuration for Material Ui inside the lib folder. The issue arises when I try to integrate Storybook within the mui folder. Unfortunately, ...

What steps can I take to hide my textarea after it has been clicked on?

Recently, I reached out for help on how to make my img clickable to display a textarea upon clicking. While I received the solution I needed, I now face a new challenge - figuring out how to make the textarea disappear when I click the img again. Each clic ...

Is there a way to present both Javascript and templates from parallel directories using express?

QUERY: Is there a way to modify the server.js file so that the three.js script in index.html does not display an error message like Cannot GET /node_modules/three/three.js? Code Snippet: index.html <!DOCTYPE html> <html lang="en"> <head&g ...

Keep fancybox items in their designated spot

When the window is opened at full size, everything looks fine. However, when you resize the window horizontally, the thumbnails shift and disappear. I want them to remain fixed in their position like the large pop-up image so that users can still see them ...

Delay the closure of a window by utilizing a straightforward method when selecting the "X - CLOSE" option with the following code: `<a href="javascript:window.open('', '_self').close();">X - CLOSE</a>`

I need to implement a delay when users click on the link to close a window. The purpose of this delay is to allow time for playing random "signoff" audio files, such as "Thanks!" or "See you next time!". Currently, without the delay, the audio stops abrupt ...

Creating a platform for users to share their thoughts and engage in

A friend and I are working on creating a commenting system for our website. We have written some code to insert values into a mysql database so that they can be read and displayed as comments later on. Unfortunately, we are facing an issue where the data i ...

Material-UI CardMedia is experiencing difficulty in loading dynamic images from a URL, however, it is functioning properly with

I have created a function that retrieves image URLs based on the memType: const renderPicture = (memType, memID) => { if ((memType === "Music")) { toString(memID.music.map((albumArt) => (albumArt.albumArt))) } else if ((memT ...

An issue was encountered during the prerendering of the page "/". For more information on this error, visit: https://nextjs.org/docs/messages/prerender-error Attempting to adjust the request (unable to determine

It's been four days and I'm still stuck. I've seen some suggestions to use axios and set the timeout or switch from HTTP to HTTPS when fetching data, but nothing has worked. I'm now four days behind deadline and the client is not going ...

Tips for eliminating the "must use restructuring props" task

I have been invoking the method in this specific manner: componentDidMount() { this.props.updateFoo({a: false,b : false, c: false,d : true, e: true }); } However, I am encountering a warning stating "Must use destructuring props assignment". ...

Nuxt Js - Ensuring script is only loaded once during the initial page load

I already have a static website design, but now I'm converting it to Nuxt.js to make it more interactive. After running my Nuxt server with "npm run build...npm run start," the scripts load and my carousel/slides work fine. However, when I navigate to ...

Jquery is not displaying the background color of the progress bar

Greetings to all! I am currently working on implementing a progress bar for my website, but I am encountering an issue where the background-color is not displaying correctly on scroll. I would greatly appreciate any assistance with this matter. Below you c ...

Exploring ways to validate the existence of a class in HTML table elements

If I want to verify if each element has a specific class when creating tables and clicking on the corresponding cells above them, This is what I have tried. However, it did not work as expected. How can I make this work correctly? What am I missing her ...