Anonymous 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
}
Becomes
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