i am bit new to jQuery and I am trying to add a jQuery UI button on my appended list.
Here is the code I used, which of course; does not work π
I hope someone can help me with this thanks. π
<ul></ul>
<button id="Add" type="button">Add</button>
<script>
$(document).ready(function () {
$("#Add").button({
icons: {
primary: "ui-icon-plusthick"
},
text: true
});
$("#Add").click(function () {
$("ul").append("<li>"
+ "<span>SOME TEXT</span>"
+ "<button class='Remove' type='button'>Remove</button>"
+ "</li>");
return false;
});
$(".Remove").button({
icons: {
primary: "ui-icon-minusthick"
},
text: true
});
});
</script>
This:
only applies to
.Removeelements that are in the DOM when you call$('.Remove').button(...)and there aren’t any.Removeelements when you call it. You want to merge that into your#Addhandler so that new buttons will be jQuery-UI-ified when they’re created. Something like this:Demo: http://jsfiddle.net/ambiguous/fu9e9/
And for your (probable) next question, you’ll want to use
$(document).on(...)to hook up the “remove” buttons:Demo: http://jsfiddle.net/ambiguous/fu9e9/2/
That will hook the callback function up to all existing and future
.Removeelements.