Problem when you add a variable inside a variable

Hello everybody

I have a problem with Java Script damned
I want to add the values of several variables in one variable and then use this variable, which contains the values of variables

You can see the following example


<input type="text" name="txt1" />
<input type="text" name="txt2" />
<input type="button" name="submits" value="send"/>

Javascript Code

field = document.getElementsByName('txt1')[0];
field2 = document.getElementsByName('txt2')[0];
submit = document.getElementsByName('submits')[0];
data = 'data=' + field.value + '&data2=' + field2.value + '&submits=' + submit.value;
document.getElementsByName('submits')[0].onclick = function(){

After executing this code I find that the variables are not displayed values
As in the following picture

I want to know what the cause of this problem ?

Your welcome

You simply have your element calls in the wrong place, simply change it to the following

document.getElementsByName('submits')[0].onclick = function(){
    var field = document.getElementsByName('txt1')[0];
    var field2 = document.getElementsByName('txt2')[0];
    var submit = document.getElementsByName('submits')[0];

    var data = 'data=' + field.value + '&data2=' + field2.value + '&submits=' + submit.value;


You are really a great man
Thank you very much Mr: SgtLegend