1

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()">
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Simone Melloni
  • 430
  • 1
  • 6
  • 16

6 Answers6

2

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

Mark Walters
  • 12,060
  • 6
  • 33
  • 48
  • 1
    Actually function(){} is perfectly valid. It creates an anonymous function. The problem is you can't call it with function(), because that is not the name of the function. – MofX May 17 '13 at 08:38
1

function is a reserved keyword, you can't use it as a function name.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 2
    Well. There are anonymous javascript functions aren't there? http://stackoverflow.com/questions/1140089/how-does-an-anonymous-function-in-javascript-work But I'm guessing that's not the question at hand. – Singular1ty May 17 '13 at 08:37
  • 1
    @Singular1ty — There are anonymous functions, but if you want to call them later then you have to store them somewhere, and then you can't use `onclick="function()"` because `function` isn't an acceptable name for a function! – Quentin May 17 '13 at 08:40
  • Okay, interesting, didn't know that - but the OP didn't say whether or not he was calling the function via `onclick`. I think the issue is just element Id's being wrong. – Singular1ty May 17 '13 at 08:41
  • @Singular1ty — Yes he did! That example is copy/pasted from the question! – Quentin May 17 '13 at 08:41
  • yeah, missed that hiding down there. So anonymous functions are more like anonymous classes in Java - you can only call them for short things that you don't need to call again. Thanks for that. ;) – Singular1ty May 17 '13 at 08:43
  • No, you can pass them around and store them in variables and have them live for as long as you like. You just have to make a point of storing them and you can't store them in a variable called function. – Quentin May 17 '13 at 08:44
1
var newFunction = function(){
    var variable=document.getElementById("text");
    alert("your text "+ variable);
}
document.write('<td><input type="button" value="enter a text" onclick="newFunction()">
lfergon
  • 963
  • 15
  • 27
  • Ty this is what i was searching :) btw i forgot in function to write it correct but i know it was function NameFunction() sry – Simone Melloni May 17 '13 at 08:51
0

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.

Stefan Dunn
  • 5,363
  • 7
  • 48
  • 84
0

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.

Singular1ty
  • 2,577
  • 1
  • 24
  • 39
0

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()"