Saran Pariyar
Saran Pariyar

Reputation: 112

The value of input is not being assigned to a div

I want to get the text from the text area (id= getText), and than assign its value to the new div that I have created but the value is not being saved inside that new div.. I have tried many times but the value of the input is not saved and there is not change in when I click the button

let getText = document.getElementById("getText"); // this is     textarea
let select = document.getElementById("selectBtn"); //this is button
//this is another div
let result = document.getElementById("result");
let new_p = document.createElement("div"); //creating element
new_p.innerHTML = "";
result.appendChild(new_p); // adding into resut div  
//getting inner text of the input
let value = new_p.innerText;
//adding an event listener
select.addEventListener("click", function(e) {
    e.preventDefault();
    new_p.innerHTML = value;
    //it does not works and the value is not saved      
})

Upvotes: 1

Views: 112

Answers (1)

misterzik
misterzik

Reputation: 1870

let getText = document.getElementById("getText"); // this is textarea
let select = document.getElementById("selectBtn"); //this is button

//this is another div
let result = document.getElementById("result");
let new_p = document.createElement("div"); //creating element
new_p.innerHTML = getText.value;
result.appendChild(new_p); // adding into resut div  
//getting inner text of the input
let value = new_p.innerText;


//adding an event listener
select.addEventListener("click", function(e) {
 e.preventDefault();
 new_p.innerHTML = getText.value;
 console.log(value)
 //it does not works and the value is not saved      
})

Replace new_p.innerHTML = ""; for new_p.innerHTML = getText.value

and inside of click event,

new_p.innerHTML = getText.value;

Moving forward, try to use online code editors, to post your code, Since it might be easier for anyone to assist you, Be aware to remove any personal info from the code.

Here is a fiddle with your code https://jsfiddle.net/k8b24n3q/12/

The reason was not working, was because you were setting on click event an undefined value.

Upvotes: 1

Related Questions