javascript: get a function's variable's value within another function
nameContent
only exists within the first()
function, as you defined it within the first()
function.
To make its scope broader, define it outside of the functions:
var nameContent;
function first(){
nameContent=document.getElementById('full_name').value;
}
function second() {
first();
y=nameContent; alert(y);
}
second();
A slightly better approach would be to return
the value, as global variables get messy very quickly:
function getFullName() {
return document.getElementById('full_name').value;
}
function doStuff() {
var name = getFullName();
alert(name);
}
doStuff();
Your nameContent scope is only inside first function. You'll never get it's value that way.
var nameContent; // now it's global!
function first(){
nameContent = document.getElementById('full_name').value;
}
function second() {
first();
y=nameContent;
alert(y);
}
second();