Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Pass element to function

Hi I have a function that i want to pass an element to via clicking the element or calling the function with the element as a paramter. This is what I have:

function change_btm_cat(item) {
        $('div.prod_matrix').removeClass('block');
        $('div.prod_matrix').addClass('none');
        $('div.prod_matrix#'+item.attr('id')).removeClass('none');
        $('div.prod_matrix#'+item.attr('id')).addClass('block');
        $('div.btm_cat').removeClass('green_grad');
        $('div.btm_cat').addClass('grey_grad');
        item.removeClass('grey_grad');
        item.addClass('green_grad');
        //ps. i know its messy, just testing stuff
    }

I can easily get it to work using this:

$('div.btm_cat').click(function() {
        change_btm_cat($(this));
    });

But when i try:

$('another.element').live('click', function() {
   change_btm_cat($('div.btm_cat#7'));
});

It seems to pass nothing to the function.

See jsFiddle for working example: http://jsfiddle.net/rb7ZG/1/

like image 505
Charlie Sheather Avatar asked Mar 15 '11 01:03

Charlie Sheather


1 Answers

try:

$('div.btm_cat').click(change_btm_cat);

function change_btm_cat()
{
     $(this) //becomes available
}

and

$('another.element').live('click', change_btm_cat);
like image 142
Rob Avatar answered Oct 19 '22 22:10

Rob