How to Copy Data from Two Fields into One Variable in JavaScript?

Estimated read time 1 min read

You can copy data from two fields into one variable in JavaScript by getting the values of the fields and concatenating them. For example, let’s say you have two input fields with ids field1 and field2:

<input type="text" id="field1">
<input type="text" id="field2">

You can copy the data from these fields into one variable in JavaScript using the following code:

let field1 = document.getElementById("field1").value;
let field2 = document.getElementById("field2").value;
let combined = field1 + field2;

console.log(combined);

In this code, we first use document.getElementById() to get references to the two input fields, and then use the value property to get the values entered by the user.

Next, we concatenate the values of field1 and field2 using the + operator and store the result in the combined variable.

Finally, we log the value of the combined variable to the console.

This is just one example of how you can combine data from two fields into one variable. You can use similar techniques to combine data from fields of other types, such as select elements or checkboxes.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply