jquery - get value from textbox does not work -
for javascript code tried 2 alternative solutions in get value in input text box, answer 2 rjd22, pls see code below. option works fine, option b doesnt; console shows 'undefined' when enter figure (or text).
any idea why b not work? use option want validate form field has been filled in, , val() description (http://api.jquery.com/val/) cannot see val() returns when no value has been filled in.
thnx comments in advance.
html code:
</label> <input id="usage" type="text" name="usage">
javascript code:
this.usage = $("#usage").val(); //option a: works this.usage = $("#usage").attr("value"); //option b: doesnt work console.log(this.usage);
val()
returns ''
(empty string) when no value has been entered. should use val()
instead of prop
or attr
.
Comments
Post a Comment