Display user input on page

Ved Tiwari :

I'm having this trouble for a while and I can't quite figure it out. I want to display user input as an h1 element. How do I do this?

let allAssignments;

if (localStorage.getItem("everyAssignment")) {
  allAssignments = JSON.parse(localStorage.getItem("everyAssignment"))
} else {
  allAssignments = []
}

function submitFunc() {
  let names = document.getElementById("name").value;
  allAssignments.push(names)
  localStorage.setItem("everyAssignment", JSON.stringify(allAssignments));
  let finalArray = JSON.parse(localStorage.getItem("everyAssignment"));
}
<button onclick="submitFunc()">Submit</button>
<input placeholder="Enter Name Here" id="name">

I want to create a div element for each item in the local storage, and use .innerHTML to insert an h1 element with the name of each value in the array. How do I do this?

Ramesh :

You can do that like this:

const submitFunc = () => {
  const value = document.getElementById('name').value;
  const parent = document.createElement('div');
  const pTag = document.createElement('p');
  pTag.innerText = value;
  parent.appendChild(pTag);
  document.body.appendChild(parent);
}
<button onclick="submitFunc()">Submit</button>
<input placeholder="Enter Name Here" id="name">

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=405709&siteId=1