I need to call a variable inside a document.write
but it doesnt work...
example
function(){
var variable=document.getElementById("text");
alert("your text "+ variable);
}
inside a table there is:
document.write('<td><input type="text" id="example"></td>');<br>
document.write('<td><input type="button" value="enter a text" onclick="function()">
I need to call a variable inside a document.write
but it doesnt work...
example
function(){
var variable=document.getElementById("text");
alert("your text "+ variable);
}
inside a table there is:
document.write('<td><input type="text" id="example"></td>');<br>
document.write('<td><input type="button" value="enter a text" onclick="function()">
Share
Improve this question
edited May 17, 2013 at 8:35
Quentin
945k132 gold badges1.3k silver badges1.4k bronze badges
asked May 17, 2013 at 8:34
Simone MelloniSimone Melloni
4301 gold badge6 silver badges16 bronze badges
2
-
1
The id seems to be incorrect... It should be :
document.getElementById("example")
. – Samuel Caillerie Commented May 17, 2013 at 8:38 -
It is never a good idea to use
document.write
. Have a look at innerHTML, insertBefore and appendChild – symcbean Commented May 17, 2013 at 8:46
6 Answers
Reset to default 2Anonymous functions like below are allowed in javascript but we cannot call them by using function(){}
from an element etc at a later time. They need to have a reference, in your case just a name after function
will do
function(){
//Your code
}
Bees
function myFunction(){ //myFunction can be changed to another more suitable name
//Your code here;
}
Then from your document.write
statement call your named function in the onclick
event
document.write('<td><input type="text" id="example"></td>');<br>
document.write('<td><input type="button" value="enter a text" onclick="myFunction()">
now that you aren't using function()
which is a reserved word in javascript but using myFunction()
which javascript now thinks is your named function, it should work
var newFunction = function(){
var variable=document.getElementById("text");
alert("your text "+ variable);
}
document.write('<td><input type="button" value="enter a text" onclick="newFunction()">
function
is a reserved keyword, you can't use it as a function name.
You'll need to reference which property of the Element you want to read. Simply using getElementById will return the object, not the value of the textfield. Use getelementById('text').value
.
Not sure how much of your code there is real, and how much is just an example, but you need to double-check for your getElementById()
and check that the ID you are providing is actually in the HTML.
In your example you show getElementById("text")
but then don't have any HTML with an ID of text
Also, if you want to extract the content that is being stored inside that variable, you might want to get its value via getElementById("text").value
The main issue though, seems to be that you're using function(){}
- you should name your function, for instance: function foo(){}
and then use onclick='foo()'
to call it.
function
is a reserved keyword.
You can use it like this
var func = function(){
var variable=document.getElementById("text");
alert("your text "+ variable);
}
onclick="func()"