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

HTML Code


<input type="text" name="txt1" />
<br/>age 
<input type="text" name="txt2" />
<br/><br/>
<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(){
    alert(data);
}

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;

    alert(data);
}

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