Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I condense multiple jQuery functions into one?

Tags:

jquery

I'm fairly new to jQuery still and am trying to pick up ways to help optimize my code. I'm currently working on an application in which I'm calling some calculation methods everytime someone leaves a field (.blur). I only want to call these methods when certain criteria are met (such as value != 0). I have 9 fields where I'm calculating and checking currently.

$(document).ready(function () {
var currentValue = {};

$("#txtValue1").focus(function () {
    currentValue = $(this).val();
}
).blur(function () {
    $("#txtValue1").valid();
    if (currentValue != $("#txtValue1").val() && $("#txtValue1").val() != "") {
        CallCalculations();
    }
});

$("#txtValue2").focus(function () {
    currentValue = $(this).val();
}
).blur(function () {
    $("#txtValue2").valid();
    if (currentValue != $("#txtValue2").val() && $("#txtValue2").val() != "") {
        CallCalculations();
    }
});
});

function CallCalculations() {
    // Do Stuff
};

I know it's possible to condense these functions down into one more generic one (using a CSS class as a selector instead of an ID) but I just can't seem to figure it out as I'm still new to jQuery / Javascript in general. Any help would be greatly appreciated. Thank you!

like image 829
Delebrin Avatar asked Aug 03 '10 15:08

Delebrin


People also ask

How to combine 2 functions in jQuery?

The jQuery merge() method together merges the content of two arrays into the first array. This method returns the merged array. The merge() method forms an array containing the elements of both arrays. If we require the first array, we should copy it before calling the merge() method.

How to merge data in jQuery?

This merge() Method in jQuery is used to merge the contents of two arrays together into the first array. Parameters: The merge() method accepts only one parameter that is mentioned above and described below: first : This parameter is the first array-like object to merge, the elements of second added.

What is $( function () in jQuery?

Specifies a selector expression, element or a jQuery object to match the current set of elements against. Returns true if there is at least one match from the given argument, and false if not. function(index,element)

How do I run a function once in jQuery?

jQuery one() method The one() method attaches one or more event handlers for the selected elements and specifies a function to run when the event occurs. When using the one() method, the event handler function is only run Once for each element.


2 Answers

You can combine you id selectors like this:

$("#txtValue1, #txtValue2").focus( //etc...

Or you can use a CSS selector like this (just set the class on the relevant HTML elements as you would any other class):

$(".txtValue").focus( //etc...

And inside the blur function you can refer to $(this) instead of recalling the selection.

Final result.

$(".txtValue").focus(function () {    
    currentValue = $(this).val();    
}    
).blur(function () {    
    $(this).valid();    
    if (currentValue != $(this).val() && $(this).val() != "") {    
        CallCalculations();    
    }    
});
like image 123
DMA57361 Avatar answered Oct 05 '22 06:10

DMA57361


Firstly, you don't need to do the value caching on focus and blur. You can use change().

If you were to asign a class to all your textboxes you want checking... eg:

<input type="text" class="calculateOnChange" />

then you can use a class jQuery selector:

$('.calculateOnChange').change(function() {
    if($(this).val() != '') {
        CallCalculations(this);
    }
});

Or more generally, you could apply to each text box in the document with:

$(':input[type=text]').change( /* ...etc */ ));
like image 31
fearofawhackplanet Avatar answered Oct 05 '22 07:10

fearofawhackplanet