Retrieve information from a single text input and transfer it to a different form input using the innerHTML property

Is there a different method I should use to pass a value from one input box to another?

document.getElementById('firstForm2').innerHTML = first; 

The above code snippet is not working for passing the value, any suggestions on what method I should try instead?


            function gerdata() {
                //Collect First form Data 
                var first = document.getElementById("firstForm1").value;
                var last = document.getElementById("lastForm1").value;
                var phone = document.getElementById("phoneForm1").value;

                // transfer to 2nd Form
                document.getElementById('firstForm2').innerHTML = first;
                document.getElementById('lastForm2').innerHTML = last;
                document.getElementById('phoneForm2').innerHTML = phone;
            }
        
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    
  
    <br><br><br><br>

    <form action="">
        <h3>Form 1</h3>

        <label >First Name</label>
        <input id="firstForm1" type="text"><br>

        <label>Last Name </label>
        <input id="lastForm1" type="text"><br>

        <label> Phone Number</label>
        <input id="phoneForm1" type="text">
    </form>


    <br><br>



    <form action="" >
        <h3>Form 2</h3>

        <label >First Name</label>
        <input id="firstForm2" type="text"><br>

        <label>Last Name </label>
        <input id="lastForm2" type="text"><br>

        <label> Phone Number</label>
        <input id="phoneForm2" type="text">
    </form>
<br><br>
    <button onclick="gerdata()">transfer data to 2nd Form</button>
    <br><br><br><br><br><br><br><br>

Answer №1

My opinion is that the correct attribute to update in input fields is 'value' instead of 'innerHTML'

document.getElementById('firstForm2').value = first;

Answer №2

When updating the value of another input field, make sure to use value instead of innerHTML like this:

document.getElementById('firstForm2').value = first;

Here is a demonstration:

function updateData() {
            // Get data from the first form
            var first = document.getElementById("firstForm1").value;
            var last = document.getElementById("lastForm1").value;
            var phone = document.getElementById("phoneForm1").value;

            // Transfer data to the second form
            document.getElementById('firstForm2').value = first;
            document.getElementById('lastForm2').value = last;
            document.getElementById('phoneForm2').value = phone;
        }
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <form action="">
        <h3>Form 1</h3>

        <label >First Name</label>
        <input id="firstForm1" type="text"><br>

        <label>Last Name </label>
        <input id="lastForm1" type="text"><br>

        <label> Phone Number</label>
        <input id="phoneForm1" type="text">
    </form>


    <form action="" >
        <h3>Form 2</h3>

        <label >First Name</label>
        <input id="firstForm2" type="text"><br>

        <label>Last Name </label>
        <input id="lastForm2" type="text"><br>

        <label> Phone Number</label>
        <input id="phoneForm2" type="text">
    </form>
<br><br>
    <button onclick="updateData()">Transfer Data to Form 2</button>
    <br><br><br><br><br><br><br><br>

Answer №3

To replace the innerHTML, utilize value for the input

function transferData() {
  // Get data from First Form 
  var first = document.getElementById("firstForm1").value;
  var last = document.getElementById("lastForm1").value;
  var phone = document.getElementById("phoneForm1").value;

  // Transfer to 2nd Form
  document.getElementById('firstForm2').value = first;
  document.getElementById('lastForm2').value = last;
  document.getElementById('phoneForm2').value = phone;

}
<form action="">
  <h3>Form 1</h3>

  <label>First Name</label>
  <input id="firstForm1" type="text"><br>

  <label>Last Name </label>
  <input id="lastForm1" type="text"><br>

  <label> Phone Number</label>
  <input id="phoneForm1" type="text">
</form>


<br><br>



<form action="">
  <h3>Form 2</h3>

  <label>First Name</label>
  <input id="firstForm2" type="text"><br>

  <label>Last Name </label>
  <input id="lastForm2" type="text"><br>

  <label> Phone Number</label>
  <input id="phoneForm2" type="text">
</form>
<br><br>
<button onclick="transferData()">Transfer Data to 2nd Form</button>

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

What is the method for extracting JavaScript code as data from a script tag?

I have a file external (let's say bar.js) function qux() {} Then in my webpage, I include it using the script tag: <script type="text/javascript" src="bar.js"></script> I am looking for a way to retrieve the JavaScript code from within ...

Using JQuery and JavaScript to store and dynamically apply functions

I have a code snippet that looks like this:. var nextSibling = $(this.parentNode).next(); I am interested in dynamically changing the next() function to prev(), based on a keypress event. (The context here is an input element within a table). Can someo ...

transmit information from Node.js to Python and display the results

I've been attempting to establish communication between Node.js and Python. My goal is to send an array of objects to Python and then print it out in Python, but unfortunately my code isn't functioning as expected. content=[ { "username": " ...

Exploring directory organization in GraphQL Queries using GatsbyJS

In my portfolio, I have organized my work into categories, pieces, and pictures in a cascading order similar to a child-parent relationship. The folder structure reflects this hierarchy, with the main problem being explained in more detail below. Folder s ...

Troubleshooting Typescript app compilation problem in a Docker environment

I am encountering a challenge while trying to build my typescript Express app using Docker. Surprisingly, the build works perfectly fine outside of Docker! Below is the content of my Dockerfile: FROM node:14-slim WORKDIR /app COPY package.json ./ COPY yarn ...

What is the process for applying a border to the chosen image within the ImageList of the MaterialUI component?

Currently, I have set up the images in a grid format using the and components from MaterialUI. However, I am looking to implement an additional feature where when a user clicks on a specific image from the grid, a border is displayed around that select ...

Exploring the number of checked checkboxes within a dynamic list using Ionic 3 and Angular

I'm developing an application where I need to display a dynamic number of checkboxes and determine how many are checked before enabling a button. Some suggestions recommend using myCheckbox.isChecked() to check each checkbox individually, but since ...

Convert Ajax null value to NoneType in web2py

Every time I save information on a page, an AJAX request is sent with all the necessary data to be stored in the database. The format of this data looks similar to this example: {type: "cover", title: "test", description: null, tags: null} However, when ...

Tips for designing a sophisticated "tag addition" feature

Currently, I am enhancing my website's news system and want to incorporate tags. My goal is to allow users to submit tags that will be added to an array (hidden field) within the form. I aim to add tags individually so they can all be included in the ...

Determine whether a value is present in a JavaScript object in real-time while typing in a TextBox with the help of angular

Within a textbox, users have the freedom to input any value. Upon each keystroke, I must validate whether that value already exists in $scope.arrayObject. A common approach involves attaching a key-up event handler to the textbox and performing the necessa ...

Issues with basic routing in Angular version 1 are causing problems

I'm encountering an issue while setting up my first Angular app with routing. It seems to be a simple problem but it's not working properly. Whenever I click on the links, the URL changes. index.html - file:///C:/Users/me/repos/angularRouteTes ...

Managing SQLite errors when using JavaScript and ExpressJS

I have developed a Backend route to retrieve games based on specific letters provided. Below are the two routes that I implemented: router.get("/public/gamelist/:letter", (req, res, next) => { var sql = "SELECT title FROM Games WHERE ti ...

Use AngularJS to extract information from Wikipedia and display it

Just starting out with angularjs and attempting to pull data from Wikipedia to display on the front end. I managed to fetch the data using the php code below: $url = 'http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&a ...

Session is required for req.flash() function in node.js to work properly

I recently started working with Node.js and I'm encountering an issue with sessions. I developed a simple application and tried to run it locally, but ran into some errors. Below are the details of my code along with the error messages: BAPS.js (app. ...

The DBref information is not being displayed

I am encountering an issue with my mongoose model schema and the data in MongoDB. I am looking to retrieve specific data by using the find() method based on desired parameters. const mongoose = require('mongoose'); const sessionSchema = new mong ...

When invoking a function, a React Component utilizes the props from the first element instead of its own

Whenever I try to invoke a function of a component, it seems to be replacing the parameters and passing the props of the first array element instead of the selected one. To illustrate this issue, let's take a look at some code: Firstly, here is how ...

What is the best way to identify duplicate keys in fixed JavaScript objects?

My approach so far has been the following: try { var obj = {"name":"n","name":"v"}; console.log(obj); // outputs { name: 'v' } } catch (e) { console.log(e); // no exceptions printed } My goal is to detect duplicate keys within a ...

Manipulate and sort a collection of elements in Javascript

The following objects need to be filtered: list= [{app: "a1", company: "20", permission: "All"}, {app: "a1", company: "21", permission: "download"}, {app: "a2", company: "20", ...

Unfortunately, the Ajax functionality is not performing as expected

What am I missing? 2.php: <button id = "Text_Example">Display</button> <script src="jquery-3.1.1.min.js"></script> <script src="js_code.js"></script> js_code.js: $("#testtext").bind("click", displayNewData); functio ...

What is the process for creating a parent container in which clicking anywhere inside will cause a child container (built with jQuery UI draggable) to immediately move to that location?

This is a rundown of tasks that I am struggling to code more effectively: When the bar is clicked anywhere, I want the black dot button to instantly move there and automatically update the displayed percentage below it. Additionally, when I drag the butt ...