I have an unordered list with elements and I want to append an item at the end. Here is the current code:
The initial list:
<ul id="all">
<li>
Some text <input type="button" class="remove" value="-" />
</li>
<li>
Some text <input type="button" class="remove" value="-" />
</li>
</ul>
The code that removes a list item:
$(".remove").click(function() {
$(this).parent().remove();
});
The code that adds a new list item:
$("#add").click(function() {
$("#all").append(
"<li>"
+ "Some text"
+ "<input type=\"button\" class=\"remove\" value=\"-\" />"
+ "</li>"
);
});
The button to add a new list item:
<input type="button" id="add" value="Add" />
When I click the button, a new list indeed is added to the list, but clicking the remove button doesn't do anything.
How do I make this work?
Bonus: Change "Some text" with "<input type="text" /> <input type="text" />
" and you will see that the two newly added input elements will have different distance between them from the initial ones. Why? (NOTE: using Firefox 3.0.5).