13

Possible Duplicate:
How do I get the value of a textbox using jQuery?

on form submit, i am trying to grab the value of the textbox below and shove it in an url

<input type="text" style="width: 300px" id="fromAddress" name="from" value="" />&nbsp;

here is my jquery code:

<script type='text/javascript'>
     $(document).ready(function() {

         $(":submit").click(function(e) {

             var from = $("input#fromAddress").text;
             $('div#printdirec').html('<a target="_blank" href="http://maps.google.com/addr=' + from + '&daddr">Print Directions</a>');

         });

     });
</script>

when i look at the URL, it doesn't have the correct from address.

Community
  • 1
  • 1
leora
  • 188,729
  • 360
  • 878
  • 1,366

2 Answers2

46

You need to use the val() function to get the textbox value. text does not exist as a property only as a function and even then its not the correct function to use in this situation.

var from = $("input#fromAddress").val()

val() is the standard function for getting the value of an input.

Darko
  • 38,310
  • 15
  • 80
  • 107
3

Just Additional Info which took me long time to find.what if you were using the field name and not id for identifying the form field. You do it like this:

For radio button:

var inp= $('input:radio[name=PatientPreviouslyReceivedDrug]:checked').val();

For textbox:

var txt=$('input:text[name=DrugDurationLength]').val();
Yoosaf Abdulla
  • 3,722
  • 4
  • 31
  • 34