Nogus
Nogus

Reputation: 1

Get "value" and use it in another function

I have a little problem in JavaScript. I'm doing a gallery page. There is a icon with "Onclik" that calls function "comment".

function comment() {

var div = document.getElementById("commentdiv");
var input = document.createElement("textarea");
var btn = document.createElement("BUTTON");
var t = document.createTextNode("ADD");  

input.name = "post";
input.maxLength = "5000";

btn.appendChild(t);
document.body.appendChild(btn);  

div.appendChild(input); 
div.appendChild(btn);
btn.style.width='100%';
btn.style.height='30px';

input.style.width='100%';
input.style.height='150px';

msn = input.value;
btn.onclick=write;

}

But there is problem. When I do input.value, there isn't nothing in input. I want in msn, the text that people write in the textarea. When I calling the second function, doesn't write because is empty.

function write() {


 document.getElementById("commentdiv").innerHTML += + msn ;

}

Thank you :)

Upvotes: 0

Views: 52

Answers (3)

Jamil
Jamil

Reputation: 939

Modify the write function to be :

function write() {
   document.getElementById("commentdiv").innerHTML += input.value ;
}

Upvotes: 0

Fatih
Fatih

Reputation: 810

You have to use msn as global variable. Also remove + left of the msn

var msn;
function comment() {
   msn = input.value;
}

function write() {
   document.getElementById("commentdiv").innerHTML += msn;
}

Upvotes: 0

gurvinder372
gurvinder372

Reputation: 68413

Because msn value was taken when element was just created, you need to take fresh values from the textarea

function write() 
{
   document.getElementById("callate").innerHTML += document.querySelector( "#commentdiv textarea" ).value ;
}

Upvotes: 1

Related Questions