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 $ ('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?

Maybe setting $ ('input'). Val () will be the best in a variable?

Thank you!

Just do this:

  var inputValue = $ ('input '). Val ();  

Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -