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

117
Views
Get .innnerhtml and alert text from form

I am trying to create form where I can submit two answers.

Alongside the form I would like to create an alert where the alert takes the input from the form when one of the buttons are clicked. That works fine.

Alongside the form I would like to create another button that displays the text from the form in html.

function htmlText(argument) {
  document.getElementById("fname").innerHTML = alertText();
  document.getElementById("lname").innerHTML = alertText();
}

function alertText(argument) {
  return "Your name is: " +
    document.getElementById("fname").value + " " +
    document.getElementById("lname").value + ".";
}
<form>
  <label>Name :</label><input type="text" id="fname"> <br>
  <label>Name2 :</label><input type="text" id="lname"> <br>
</form>

<button onclick="htmlText()">html</button>

<button onclick="window.alert(alertText())">alert</button>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You likely meant to do this

Note the user of recommended eventListeners instead of inline event handlers

window.addEventListener("load", function() { // on page load
  function getText() {
    return "Your name is: " +
      document.getElementById("fname").value + " " +
      document.getElementById("lname").value + ".";
  }
  document.getElementById("htmlButton").addEventListener("click", function() {
    document.getElementById("fullName").innerHTML = getText();
  });
  document.getElementById("alertButton").addEventListener("click", function() {
    alert(getText());
  });

});
<form>
  <label>Name :</label><input type="text" id="fname"> <br>
  <label>Name2 :</label><input type="text" id="lname"> <br>
</form>

<button type="button" id="htmlButton">html</button>

<button type="button" id="alertButton">alert</button>

<sid id="fullName">
  </div>

about 4 years ago · Juan Pablo Isaza 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!