Reputation: 7102
I have a form that the user can enter in a title and a date. When the user clicks the 'Add New' button, the contents of the form should be displayed above. That parts works, but whenever they enter in new info, the previous content is just replaced...where it should add to the list.
Any ideas on how to get it to work?
Thanks
Here is the jsFiddle: http://jsfiddle.net/beqqC/1/
Here is the code:
$("#add").on('click', function () {
$(".title").html($("[name=title]").val());
$(".date").html($("[name=date]").val());
$("#view").show();
});
Upvotes: 0
Views: 68
Reputation: 3642
You should use append()
method. Just put a line break before if you want vertical list
$("#add").on('click', function () {
$(".title").append("<br />" + $("[name=title]").val());
$(".date").append("<br />" + $("[name=date]").val());
$("#view").show();
});
Upvotes: 1
Reputation: 29241
You can use a combination of jQuery's append and element creation methods to make things slightly cleaner:
$("#add").on('click', function () {
$(".title").append(
$('<div>', {
text: $("[name=title]").val(),
className: 'title'
}));
$(".date").append($('<div>', {
text: $("[name=date]").val(),
className: "date"
}));
$("#view").show();
});
This will create a div with the specified class within your .title
and .date
divs.
Upvotes: 1
Reputation: 40068
Use append, and perhaps add a <br />
code to the end:
$("#add").on('click', function () {
$(".title").append($("[name=title]").val()+'<br />');
$(".date").append($("[name=date]").val()+'<br />');
$("#view").show();
});
Upvotes: 1
Reputation: 2534
Change
$(".title").html($("[name=title]").val());
$(".date").html($("[name=date]").val());
To
$(".title").append($("[name=title]").val());
$(".date").append($("[name=date]").val());
Upvotes: 2