Global variables that can be used in multiple jQuery functions

Posted by YouBook on Stack Overflow See other posts from Stack Overflow or by YouBook
Published on 2011-01-13T23:39:48Z Indexed on 2011/01/13 23:53 UTC
Read the original article Hit count: 223

Filed under:
|

Can you be able to contain variables that can be used in multiple functions in jQuery, example:

var self = $(this);
var box = self.parents('.box');

$('#title').click(function()
{
    self.css('background', 'red');
    box.slideDown('slow');
}).dblclick(function()
{
    self.css('background', 'green');
    box.slideUp('slow');
});

So that self and box can be used within these event functions so I don't have to keep doing this:

$('#title').click(function()
{
    var self = $(this);
    var box = self.parents('.box');

    self.css('background', 'red');
}).dblclick(function()
{
    var self = $(this);
    var box = self.parents('.box');

    self.css('background', 'green');
});

But question is, is it possible, if so, how can you do that?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about global-variables