I'm attempting to get the value of text typed by a user in an input box but the problem is that instead of getting the value which the user has typed, I get the preset value of the input which is 'undefined'.
HTML
<input type="text" id="userInput" value="" name="title"
placeholder="Enter the name here" required="required"/>
<input type="button" id="text_value" value="Set Input"/>
<!-- I have this button here because I think maybe
I should have a button to change default value but
I don't know the javascript to do this -->
Javascript
// Gets input value id
var theuserInput = $("#userInput").val();
The reason I haven't shown query string code is because the input value is passed along in the url but the problem is that the default input value 'undefined' is passed instead of actual user input.
Any solutions?
ADDITIONAL CODE
Ok so here is the querystring, when you click the 'pass' button the input is passed along in querystring:
$('.pass').click(function() {
window.location.href = 'http://lala.com/passing.php?input=' + theuserInput + '';
return false;
});
theuserInput?theuserInputwithin the click handler for the buttontext_valueor somewhere else? If somewhere else, where exactly? If you are doing it on page load, the value is definitely going to be undefined. You need to make sure you are defining the variable when there actually is some text in the box. Or do you want a method of updating the variable whenever something is entered in the box? Please reply by using@ṧнʊß:)var theuserInput = $("#userInput").val();wherever you've put it currently. Then, paste the code from the fiddle at the top of your document. It should work.