0

I want to remove each selected element with same class element. My jquery coding is given bellow

$(".del_layer").each(function(){        $(this).click(function(){
           $(this).parent().remove();
                });

            });

my html structure is

<li class="select_layerList" data-refind="0">
<img width="20px" src="tmp_card_imgs/a.png">
Layer 0:Image
<span class="del_layer" style="cursor:pointer;">X</span>
</li>
<li class="select_layerList" data-refind="0">
<img width="20px" src="tmp_card_imgs/b.png">
Layer 0:Image
<span class="del_layer" style="cursor:pointer;">X</span>
</li>

But each function is not working. How can I solve this

2
  • 1
    You don't need to keep your .click() inside the .each(). Commented Jun 21, 2013 at 9:41
  • looks fine to me jsfiddle.net/arunpjohny/hcB4T/1 Commented Jun 21, 2013 at 9:41

5 Answers 5

1

It's working fine.

http://jsfiddle.net/4LeuA/

Ensure you have the DOM has loaded, place your code inside a document.ready()

$(document).ready(function() {

});
Sign up to request clarification or add additional context in comments.

Comments

0

You can remove each function because you can select them via class name and put your script on document ready like

$(document).ready(function(){
    $('.del_layer').on('click',function(){
        $(this).parent().remove();
    });   
});

Comments

0

Why not try something like this

 $(".del_layer").on('tap',function(){
  $(this).parent().remove();
 });

Comments

0

There is no need to traverse through .each(), just apply the event to the selector and remove its parent:

$(".del_layer").click(function(){
    $(this).parent('li').remove();
});

and you can use .closest() function to traverse up to the desired parent.

$(".del_layer").click(function(){
    $(this).closest('li').remove();
});

Comments

0

Using event delegation, you can set the same handler once:

$(document).on('click', '.del_layer', function() {
  $(this).parent().remove();
});

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.