How to pass the contents of a text box as a string

  • Thread starter Thread starter AAaron123
  • Start date Start date
A

AAaron123

<input id="txtStart" type="text" name="txtStart" value="620 Rugter Street ,
Utica, NY,13501" style="width:150px;"/>

<input id="txtEnd" type="text" name="txtEnd" value="168 Genesee Street,
Utica, NY,13502" style="width:300px;"/>

<input id="btnFind" type="button" name="btnGetRoute" value="Find"
onclick="GetRoute(txtStart.value,'168 Genesee Street, Utica, NY,13502');"
style="width:200px;"/>

The question relates to the button. If I use for both argument a string
like I used for the second argument it works OK.



But when I try to use the values in the text boxes, like I show for the
first argument, I can't get it to work.

The routine in the head starts with

function GetRoute(start, end) {

alert(start);

alert displays "undefined"



I don't want to send a reference to the textbox.

I want to send the contents as a string.



Thanks
 
Use something like the following:

onclick="GetRoute(document.getElementById('txtStart').value,'168 Genesee
Street, Utica, NY,13502');"

Whenever you reference an element using JavaScript, you should use the
document.getElementById() method. In some cases, the id of an element will
be automatically generated by ASP.NET. In this case, you will need to
dynamically generate the JavaScript using the ClientID property of the
Control class (because Control is inherited by the WebControl class, all the
WebControls such as TextBox have this property available). Hopefully all
this helps! Good Luck!
 
Thanks, that worked.


Nathan Sokalski said:
Use something like the following:

onclick="GetRoute(document.getElementById('txtStart').value,'168 Genesee
Street, Utica, NY,13502');"

Whenever you reference an element using JavaScript, you should use the
document.getElementById() method. In some cases, the id of an element will
be automatically generated by ASP.NET. In this case, you will need to
dynamically generate the JavaScript using the ClientID property of the
Control class (because Control is inherited by the WebControl class, all
the WebControls such as TextBox have this property available). Hopefully
all this helps! Good Luck!
 
Back
Top