tags:

views:

65

answers:

6

Hey all,

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!

+5  A: 

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();    
    }    
});
DMA57361
Thank you for the answer. I knew I could combine selectors into one statement but the referencing using $(this).val() is where I got slightly confused (since I was technically leaving the field I kept thinking it wouldn't work properly).
Delebrin
A: 

Give your elements a class like textValues and then you can do this:

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

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

function CallCalculations() {
    // Do Stuff
};
PetersenDidIt
A: 

You could refactor it like this for both inputs:

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

Don't worry too much about premature optimization, for starters. On the other hand, it never hurts to keep your code clean, don't repeat yourself, all that.

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

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

function CallCalculations() {
    // Do Stuff
};
Matt Ball
A: 

You can consolidate similar things like this:

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

  $("#txtValue1, #txtValue2, #txtValue3, #txtValue4").focus(function() {
    currentValue = $(this).val();
  }).blur(function() {
    $(this).valid();
    if (currentValue != $(this).val() && $(this).val() != "") {
      // DO STUFF??
    }
  });
});

I don't know if that is what you are looking for?

Bob Fincheimer
+4  A: 

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 */ ));
fearofawhackplanet
+1 for answering the underlying question that the OP probably *needed* to ask.
DMA57361
Thanks. Can't believe there's 5 other answers here and no-one has noted that the whole `focus` and `blur` thing isn't required. Just goes to show you can't trust advice from the internet :)
fearofawhackplanet
@fear Indeed. Unfortunatly it's a side effect of the [fastest gun in the west problem](http://meta.stackoverflow.com/questions/9731/fastest-gun-in-the-west-problem), and in this case I happen to be guilty as well.
DMA57361
Thank you for pointing me in the right direction everyone. I knew there was likely an easier way to handle this than using focus and blur both, so this makes a lot more sense.
Delebrin