Reputation: 61
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
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
Reputation:
$(this).closest("ul").remove();
Or better yet:
$("ul.w1m").on("click","img[src='img/minus.png']",function(){
$(this).remove();
});
Upvotes: 2