How do I condense multiple jQuery functions into one? -
I'm still new to jQuery and trying to get ways to help optimize my code . I am currently working on an application in which I call some calculation methods when a person leaves the field (.blur). I only want to call these methods when some criteria are met (like value! = 0). I have 9 fields where I am calculating and currently checking.
$ (document) .ready (function () {var currentValue = {}; $ ("# txtValue1"). Function () {currentValue = $ (this) .val (); }) .blur (function () {$ ("# txtValue1"). Valid (); if (currentValue! = $ ("#TxtValue1"). Val () and & amp; $ ("#txtValue1"). Val ()! = "") {CallCalculations ();}}) $ ("#txtValue2") Focus (function () {currentValue = $ (this) .val ();}) .blur (function () {$ ("# txtValue2"). Valid (); if (currentview! = $ ("#TxtValue2") Val () & $ ("#TxtValue2"). Val ()! = "") {CallCalculations ();}})}}; Function call calculation () {// Do Stuff};
I know that these functions are possible to do one more normal one below (selecting the CSS class as a selector instead of an ID), but I can not find it. Still new to jQuery / Javascript, any help would be welcomed. Thanks!
First of all, you do not need to cache the value and blur the focus. You can use change ()
.
If you want to assign all your text boxes as a square, then you want to check ... like:
& lt; Input type = "text" class = "calculate on change" />
Then you can use a class jQuery selector:
$ ('calculate on-change'). Change (function () {if ($ (this) .val ()! = '') {Call collation (this);}});
Or more generally, you can apply to each text box in the document with the following:
$ (': input [type = text] '). Change (/ * ... etc * / /));
Comments
Post a Comment