How to Add User Inputs from a Form to a Card Using JavaScript
Tags: javascript, beginners, webdev, css
Published: 2023-05-21 13:15:33

**Article Objective**
At the end of this article, the reader will learn how to create a form and a set of cards, then how to add the inputs from the form (created with HTML and CSS) to a specific card (also created with HTML and CSS) all on a webpage using JavaScript.

**Article Outline**
- Introduction
- Prerequisites
- Step 1: Creating a form with submit button (HTML & CSS)
- Step 2: Creating four cards and styling them (HTML & CSS)
- Step 3: Getting the form inputs (JavaScript)
- Step 4: Adding the form inputs to the empty card (JavaScript)
- Conclusion

---

**Introduction**

Forms can be created for different purposes on a website or blog. For instance, a form can be created to enable a user to subscribe to an online service or to submit a complaint on a website. Also, a form can be created to get feedback from the user of a website. So, whatever a form is meant to achieve is determined by the owner(s) of a website.

In this technical article, we are going to create a form that accepts user inputs; the form will have up to seven input fields in which a user can enter a laptop brand and their desired specifications. Thereafter, we will create a set of four cards listing laptop brands and their specifications. The first, second, and third cards are going to be pre-filled with specifications while the fourth card remains empty (input will come from the user).

Now using JavaScript, at the click of the “Submit” button, the user inputs from the form we created is then added to the empty card so that it looks like the other three cards. All of these is quite straightforward to achieve by following the steps I have outlined in this article.

Without further long talks, let’s get started.

---

**Prerequisites**

- We need to first create a form that accepts user inputs using plain HTML and CSS.

- Then we need to create a set of cards also using plain HTML and CSS.

If you are just starting out learning HTML and CSS, follow Step 1 and Step 2 closely on how to create a form and a set of cards respectively.
If you are already familiar with how to create a form and a set of cards using HTML and CSS, you can jump to Step 3 and continue.

---

**Step 1: Creating a form with submit button (HTML & CSS)**

The first thing we have to do is to open up our code editor (VS Code used here) and create three separate files which are:

- index.html
- style.css
- script.js

We then get started under HTML by entering Shift + ! or inputting html5 and hitting enter to load up the default HTML codes. The next thing to do is to link the other two files (i.e., CSS and JavaScript) to our HTML file. The following screenshot explains better.


![HTML header](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yoc2w9orunou0cyqocgy.png)

Note: ‘defer’ used in the JavaScript link is to tell the server to first load HTML and CSS files before executing JavaScript.

We proceed to create the form (using form object in HTML) inside a container and it will have the following inputs: a header, Laptop name, Processor, Generation, RAM, SSD, Screen size, and Backup.


![Form container](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jhnatsrm2i1eki6vaq7c.png)

In order to style the form and be able to access each of the input fields in JavaScript, we have to use a selector (_class name_) – as the screenshot above shows – for each of the input fields. The selectors can now be used to style the form in CSS:

![Body and form styling](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2f913t8odorxnfuxakxp.png)

![Form and button styling](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1szd67maevomp1ljpxjk.png)

The form will appear as follows:


![Form image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zpggnaqy1cre7rl2knxf.png)

We are done with creating the form and we proceed to the next step.

---

**Step 2: Creating four cards and styling them (HTML & CSS)**

Here, we are going to create four cards which will all have laptop brand specifications, out of which three of them will be pre-filled and the remaining one left blank. The four cards are each going to have the following selectors: _card1_, _card2_, _card3_, and output and be placed inside a parent container which is going to have the selector _specs-container_. Each of the four cards are going to have different image and similar styling (though with different background colors).

Here are the HTML codes to create the cards:


![HTML codes for cards](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jyadaf4jrkv4rcyjo1mh.png)


![HTML codes for card](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hr4fxjms127n43x15eew.png)

In order to style these cards, we will apply the following CSS styles:

![specs container styling](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lx76b51ecb1az1q3itjq.png)

![specs card styling](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5zs1l5zijr3bnb5p37u2.png)

Now after applying all these CSS styles, the cards should finally look like this:

![Form and cards](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gbruxt7nkxbkdtuavp6z.png)

_Note_: The laptop images displayed in the cards does not represent the real laptops with those specifications.

As you can see, the empty card is only having labels and there are no specifications indicated – which is because we want the specifications to come from the user via the form we created. But in order to transmit the user inputs in the form to the empty card, we need JavaScript and this leads us to the next step.

---

**Step 3: Getting the form inputs (JavaScript)**

Before the inputs of the form can be transmitted and displayed in the empty card, we need to write a function in JavaScript with the name we specified in the submit button onclick function in HTML i.e., submitBtn( ).


![onclick](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2a59ehkcpiuqm3qlivim.png)

Then we need to get the actual value of each input in the form using their selector (class name) and adding _.value_ in JavaScript. Since we will be using the class name of each input, we have to use _document.querySelector_.

Here are the JavaScript codes to get the form input:


![JavaScript for form input](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7liszbvdpgj3ecrkj3w3.png)

With this, we can get the actual value of whatever specifications a user enters into the form for the laptop name, processor, generation, RAM, SSD, screen size, and backup. But when the user clicks the submit button, nothing is going to happen – all the inputs will remain on the form. This is because at the click of the submit button, the form inputs goes nowhere since we haven’t specified where it should go.

Specifying where the form inputs should go is what we will treat in the next step.

---

**Step 4: Adding the form inputs to the empty card (JavaScript)**

To be able to transmit the form inputs to the empty card, we need to get the exact location of the specification that matches the form input using JavaScript. This means the output on the card must match the input from the form.
 
For us to get the actual value of each output in the empty card, we use their selector (id) and to do that, we have to use _document.getElementbyId_. To access the individual content in the card, we say _.textcontent_ and to finally transmit each form input to the output, we equate the output to the corresponding input.

Here are the JavaScript codes to transmit the form inputs to the outputs in the empty card:


![JavaScript codes for output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kspraalf6qcpol6gnocx.png)

Now with this, anything a user enters into each of the form inputs will go to the corresponding output on the card.

But there is a little problem here. The problem is that the webpage will automatically refresh itself at the click of the submit button, meaning that all the form inputs will show briefly on the card before the webpage get refreshed.

By default, the submit button refresh a webpage. But in order to prevent this from happening, we need to write a JavaScript code that prevents the submit button from refreshing the webpage which will then allow the form inputs to be transmitted to the empty card.


![JavaScript to prevent submit button default behavior](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/whx97pnxmkntx3ztr3k0.png)

So, with this, the form inputs should be transmitted to the empty card without the webpage being refreshed automatically.

![User inputs in form](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0mtrpvq2wmeje23buvkf.png)

![User inputs showing on empty card](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38u352sq1erxhsg5hstk.png)

As you can see from the second screenshot, all the input fields in the form got cleared after the submit button was clicked and the inputs transmitted to the empty card. This is because in the code above, the following was specified:

![Inputs cleared for form fields](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e36txw8wezj0lzppr8vh.png)

What this code does is to return each of the input fields in the form to null after the submit button is clicked.

Below is the code implementations for this project on my Codepen.

_Note:_ The laptop image for each card is not showing due to issues with uploading images on Codepen.

{% codepen https://codepen.io/seun_codes_writes/pen/ZEmZjyM %}
---

**Conclusion**
In this technical article, we have been able to learn about how to add user inputs from a form to a card using JavaScript. The steps taken to achieve this are pretty straightforward and it is easy to implement on a webpage. The card that was used here to display the form inputs is just the example I chose, actually you can transmit the inputs from a form to any part of your webpage. So if you are looking to build a website with a form that accepts inputs and transmit it to a card (or somewhere on your webpage), you won’t go wrong following the steps above.