Tuesday, March 1, 2011

Is it Ok to pass jquery objects to different functions?

I am doing the following :

$(function() {
    var $txt = $("#text1");

    callMe($txt, $txt.val());                


});

function callMe(t, vlue) {
    t.html(vlue)
}

I will be calling the callMe() function multiple times.

Is it Ok to do the way i have shown or is there a better way?

From stackoverflow
  • There's no problem with passing jQuery objects around.

    It will be much more efficient than re-running the selector each time - that's for sure.

    powtac : If all your code is jQuery, I think it's good design.
  • jQuery is JavaScript. And this is absolutely valid in JavaScript. You can even just pass the jQuery object an do this:

    function callMe(t) {
        t.html(t.val());
    }
    

0 comments:

Post a Comment