Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the object value on my text input?

var fname = document.getElementById("fName");
var lname = document.getElementById("lName");
const btnSave = document.getElementById("btnSave");
const btnDisplay = document.getElementById("btnDisplay");

btnSave.addEventListener("click", getData);

function getData() {
  console.log(
    getFullName({
      fname: `${fname.value}`,
      lname: `${lname.value}`,
    })
  );
}

function getFullName(FullName) {
  return FullName.fname + " " + FullName.lname;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="main.css" />
  </head>
  <body>
    <input type="fName" placeholder="First Name" />
    <input type="lName" placeholder="Last Name" />
    <button id="btnSave">Save</button>
    <button id="btnDisplay">Display</button>

    <script src="main.js"></script>
  </body>
</html>

How to get the object value on my text input?
It returns null. But when I assign value on my object like fName: "John", lName: "Doe"
It returns successfully.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Your input elements do not have ids.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading