Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
How do I save the content after the onclick?

I already connected the html with js and I get the data from a form when the onclick is activated but since I save each data that I receive to use it later, how do I get the data out of the function without having problems:

var getData = () => {
  let name = document.getElementById("name").value;
  let age = document.getElementById("age").value;

  if (name == "") {
    document.getElementById("name").focus();
  } else {
    if (age == "") {
      document.getElementById("age").focus();
    } else {
      console.log(name + " " + age);
      document.getElementById("name").value = "";
      document.getElementById("age").value = "";
      document.getElementById("name").focus();
    }
  }
};
about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

You should create an external variable to save the data you want, when the function you are using finishes, the data in it "disapears". So, in order to save it, you should create an external variable.

Anyways, should be good to know a bit more of the problem that you are facing with.

about 4 years ago · Santiago Gelvez Report

0

Some explanations in code comments:

// Define checkGetData function
const checkGetData = () => {
  // Read data from input
  const name = document.getElementById("name").value;
  const age = document.getElementById("age").value;

  if (name == "") {
    // If name is empty, focus on field
    document.getElementById("name").focus();
    // Retuen false from getData function
    return false;
  } else if (age == "") {
    // Same with age
    document.getElementById("age").focus();
    return false;
  } else {
    // If both field have value
    // Log it
    console.log(name + " " + age);
    // Reset inputs
    document.getElementById("name").value = "";
    document.getElementById("age").value = "";
    document.getElementById("name").focus();
  }
  // Return variables 'name' and 'age' from
  // getData function back to caller. Values
  // are still there, now they do not rely on
  // input fields, because you first assign
  // values to this variables from input fields
  // and only after made this input fields empty. 
  return { name, age };
};

// Define submitForm function
const submitForm = () => {
  // Call checkGetData function
  // and set it's return value to new variable
  const inputData = checkGetData();
  
  // Check inputData
  if (!inputData) {
    // If inputData false
    // show message in div
    document.getElementById("info").innerText = "Some fields are empty!";
  } else {
    // If value exist, show
    // it in div
    document.getElementById("info").innerText = `Name: ${inputData.name}, Age: ${inputData.age}`;
  }
  
  //
  // So basically now you have input values in
  // inputData variable. You can pass it to any
  // other function, or return to parent caller
  // if it exist. Obviously this additional layer
  // with function submitForm is added here just
  // for reference and you can apply any logic
  // you prefer
  //
}
Name: <input id="name" type="text"></input> 
Age: <input id="age" type="number"></input>
<button onClick="submitForm()">Submit</button>
<br/>
<br/>
<div id="info" style="color:red"></div>

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!