ilyo
ilyo

Reputation: 36411

How to select element which is not 'this'?

I have list of divs all with the same class, I want to apply a function to all of them which are not the clicked one (this), how can i select !this with jQuery?

UPDATE: I've made this and it is not working, any ideas why?

    $("li").each(function(){
        $("li").not(this).click(function(e) {
            $(this).hide();
        });
    });

UPDATE 2: this is the whole actual code:

$(".mark").click(function(e) {
    e.preventDefault();
    var id = "#" + $(this).parent().parent().parent().parent().attr("id") + " ";

    var currentStatus = "deleted"; // to be replaced with actual status
    var currentStatusClass = "." + currentStatus + "-heading";
    $(id + currentStatusClass).show();

    $(id + ".edit-headings").click(function(){
        $(this).find(".headings-status").show().addClass("bg-hover");

        $(id + ".headings-status").click(function() {
            $(id + ".headings-status").not(this).hide();
        });
    });
});

Upvotes: 5

Views: 9944

Answers (5)

thecodeparadox
thecodeparadox

Reputation: 87083

this is wrong:

var newStatus = $(this).className().replace("contribution-", "");

className is not a function.

Instead of above line you can try this:

var cls = $(this).attr('class').replace('contribution-','');
$(this).removeClass().addClass(cls);

Upvotes: 0

Dennis
Dennis

Reputation: 14495

Have a look at jQuerys not function: http://api.jquery.com/not/

$('div').not(this).doSomething();

Regarding your update, try:

        $("li").click(function(e) {
            $("li").not(this).hide();
        });

Upvotes: 7

Roko C. Buljan
Roko C. Buljan

Reputation: 206505

using :not() or .not()

$this;

function myFunction(){
      // stuff here // and use $this for element reference
}


$('yourElement:not(.selected)').on('click',function(){
    // (make sure to add '.selected' to the new one and toggle existant)
    $('.selected').removeClass('selected');
    $this = $(this);
    $this.addClass('selected');

    myFunction();
});

Upvotes: 0

Bibin Velayudhan
Bibin Velayudhan

Reputation: 3103

use $('div').not(this) to select other than clicked one.

Upvotes: 1

Anthony Grist
Anthony Grist

Reputation: 38345

Use the .not() function:

$('.yourclass').click(function() {
    $('.yourclass').not(this).yourFunc();
});

Upvotes: 5

Related Questions