1

Im trying to figure this one out, but no joy yet. For reasons that I cant go into here, I need to use both "this" and another selector to bind a click event.. Example

    $('mySelector').each(function(){
    $(this, this.siblings('img')).bind('click', function(e){
        e.preventDefault();
        doStuffTo($(this));
    });
});

But I cant figure out how to do it. Anyone have any idea how I would achieve it?

0

3 Answers 3

5

Most of the jQuery methods (that don't return a value) are automatically applied to each element in the collection, so each() is not necessary.

Combining siblings() and andSelf(), the code can be simplified to:

$('.mySelector').siblings('img').andSelf().click(function (e) {
    e.preventDefault();
    doStuffTo($(this));
});
Sign up to request clarification or add additional context in comments.

1 Comment

I like yours better...it's cleaner.
2

Instead, you could do this:

$('mySelector').each(function(){
    $(this).siblings('img').andSelf()).bind('click', function(e){
        e.preventDefault();
        doStuffTo($(this));
    });
});

Comments

1

It's not entirely clear what you're trying to do, but perhaps it's this:

var f = function(e){
    e.preventDefault();
    doStuffTo($(this));
};

$('mySelector').each(function() {
   $(this).bind('click', f);
   $(this).siblings('img').bind('click', f);
});

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.