I am still very new to JavaScript and jQuery.
I have the jQuery “add boxes” functionality working for adding dynamic <textarea>s, but the remove portion does not work.
My code:
$(function() {
var i = $('textarea').size() + 1;
$('#remove').click(function() {
if (i > 1) {
$('.this:last').remove();
i--;
}
});
$('.Add').live('click', function(e) {
$('<div><textarea id="txt"></textarea> <textarea id="txt2"></textarea></div>').fadeIn('fast').appendTo('.Option');
i++;
});
});
Demo: http://jsfiddle.net/dnwTV/
Any help would be greatly appreciated.
You are selecting
.this:last, and no elements with aclassofthisexists. Usetextarea:lastas a selector instead. Also, your markup is inconsistent; the original should have another<div>wrapping the two<textarea>s. Here is a corrected version of your jsFiddle.That said, I don’t believe your current code is either sufficiently neat or generic. See this jsFiddle for an example of how you might make this cleaner.