Not repeating selectors in a conditional statement with JavaScript/jQuery -
I have a small app with a form and an input field. When a user submits this form, then I first want to see that there are only letters in the price if everything is fine, then I want to give value to the function.
What do I have here:
$ ('form'). On ('submit', function (e) {if ($ ('input'). Val (). Match (/ ^ [a-zA-Z] + $ /)) {someFunction ($ ('' input '') Val ());} Else {// error message or something else} e.preventDefault ();});
I do not like to write Maybe setting Thank you! $ ('input'). Val ()
Twice (conditional statement once, and if it is true) using will not work, because it is within conditional statement and there is no function of any kind Is there any way to repeat the code in this scenario?
$ ('input'). Val ()
will be the best in a variable?
Just do this:
var inputValue = $ ('input '). Val ();
Comments
Post a Comment