7

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/

2 Answers 2

14

try:

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

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

and

$('another.element').live('click', change_btm_cat);
1
  • Very nice! Never realized $(this) would be in context with this technique. Slick.
    – Cory House
    Nov 8, 2012 at 4:32
4

You are going a little overboard with the selector, try this.

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

Since you have the ID and the ID is pretty much the most refined piece of unique information you can have (being the identifier) there is not need for the other refinements.

http://jsfiddle.net/rb7ZG/2/

Let me know if I missed your goal.

1
  • Ahh, i understand whats going on now. Ill just update some of my id's and hopefully should be on the way. Cheers Mar 15, 2011 at 1:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.