Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove LI from UL on click using jQuery

Tags:

html

jquery

So I have a INPUT TEXT that upon keypress=enter prepends an LI to an UL

The LI contains a LINK that gives the user the option to remove it from the UL

HTML (on page load)

<ul id="conditionList"></ul>

after ther user inputs some conditions, the UL gets populated with LI

<li>condition<a href="#" class="clearitem">x</a></li>

However, in my jQuery

$('#conditionList a.clearitem').click(function(){
$(this).parent().remove();
});

will not work!

Is it because it's not recognizing the new LI's?

like image 658
st4ck0v3rfl0w Avatar asked Jan 25 '26 21:01

st4ck0v3rfl0w


2 Answers

Deprecated since 1.9. use on instead of live.

Use live() instead of click there because your link is generated dynamically:

$('#conditionList a.clearitem').live('click', function(){
  $(this).parent().remove();
});

.live()

Attach a handler to the event for all elements which match the current selector, now or in the future.

Depracate since 1.9. use on instead of live.

like image 99
Sarfraz Avatar answered Jan 27 '26 11:01

Sarfraz


You need to use an event binding that's going to recognize created elements. This should do that:

$('#conditionList a.clearitem').live('click', function () {
  $(this).parent().remove();
});
like image 41
g.d.d.c Avatar answered Jan 27 '26 09:01

g.d.d.c



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!