View Full Version : Pulling value from a form with val();

10-21-2010, 08:19 PM
I am having a problem pulling the value from a text input from a form. The line is in bold below.

The jquery:

$(document).ready(function( numberInput ) {

var numberInput = $("#numberInput").val();

senddata = function() {

$.post("post.php", { number: numberInput }, function(data) {


The Form:

<form name="form">
<input id="numberInput" type="text">
<input onclick="senddata();" type="button" value="send data">

Anyone have any ideas?? Help would be much appreciated.

10-22-2010, 02:11 AM
Your $.post statement is inside a closure, but has a reference to the numberInput variable. Since numberInput is only set once when the document has been loaded (which would be a blank string), you'll always be getting the same value when you click the button.

Any of the 2 below should fix it...

$.post("post.php", { number: $("#numberInput").val() }, function(data) { $("#results").replaceWith(data); }); }


senddata = function() {
var numberInput = $("#numberInput").val();
$.post("post.php", { number: numberInput }, function(data) { $("#results").replaceWith(data); }); }