Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all element except one?

I would like to remove all element from my canva except the one on which I click. I create a set, put all element inside and remove the set :

button.click(function () {
    var to_remove = paper.set();    
    paper.forEach(function (el) {
        to_remove.push(el);
    });         
    to_remove.remove();
});

But i don't success to test if my element is my button or not.

Axel

like image 500
axel584 Avatar asked Nov 13 '22 14:11

axel584


1 Answers

You can simply cache your clicked element and compare it during the loop.

button.click(function() {
    var clickedEl = this,
        toRemove = paper.set();

    paper.forEach(function(el) {
        if (el !== clickedEl) {
            toRemove.push(el);
        }
    });

    toRemove.remove();
});​

Demo: http://jsfiddle.net/yRNNe/

like image 122
amustill Avatar answered Nov 15 '22 04:11

amustill