zapata
zapata

Reputation: 61

How can I remove parent element when its child is clicked

how can i remove an ul if I click one of its children which is inside of it ? I know this ul .remove not true and How can I set color of "projects" as red? I have to use .find()

<ul class="w1m">
    <li>
       <div>
         <img src="img/minus.png" onclick="$(this ul).remove();"/>
         <img src="img/link.png"/>
       </div>
       <div>
         inspiration
       </div>
    </li> 
</ul>

<ul class="w1m">
    <li>
       <div>
         <img src="img/minus.png" onclick="$(this ul).remove();"/>
         <img src="img/link.png"/>
       </div>
       <div>
         projects
       </div>
    </li> 
<ul>
    <li><div><img src="img/minus.png"/><img src="img/link.png"/></div>  <div>job board website</div></li>
    <li><div><img src="img/minus.png"/><img src="img/link.png"/></div>  <div>shopping cart</div></li>
</ul></ul>

Upvotes: 0

Views: 2254

Answers (5)

Vikas
Vikas

Reputation: 376

using the following Jquery for that

$(this).closest("ul").remove();

Upvotes: 1

madlee
madlee

Reputation: 647

first of all, the way you are adding the handler is not ideal. at the bottom of your page, in a script tag, you should add this code inside of jQuery's ready method.

<script>
  $(document).ready(function() {

    $('ul.w1m > li').on('click', function() { $(this).parent().remove(); });

  });
</script>

in your above code, the value of this would be the <img> element, and you would be trying to select the child ul of that img (which obviously doesn't exist). Also, when using the this keyword with other selectors in jQuery, you need to wrap the other selectors in quotes. (ie $(this+'ul')).

Upvotes: 1

James Donnelly
James Donnelly

Reputation: 128781

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

Upvotes: 0

user1726343
user1726343

Reputation:

$(this).closest("ul").remove();

Or better yet:

$("ul.w1m").on("click","img[src='img/minus.png']",function(){
    $(this).remove();
});

Upvotes: 2

SLaks
SLaks

Reputation: 887275

Like this:

$(this).closest('ul').remove();

Upvotes: 1

Related Questions