Reputation: 549
When I add this part of code to the script it doesn't display the Date and time:
var d = new Date();
var $fulaDate = $(d.getFullYear() + "-" + d.getMonth()
+ "-" + d.getDate() + "T" + d.getHours() + ":" + d.getMinutes()
+ ":" + d.getSeconds()).appendTo(div);
I Can't figure out what the problem is.
JQuery
function addComment(name1) {
var container = $('#divComments');
var inputs = container.find('label');
var id = inputs.length + 1;
var div = $('<div />', { class: 'CommentStyle' });
$('<label />', {
id: 'comment' + id,
text: name1
}).appendTo(div);
var d = new Date();
var $fulaDate = $(d.getFullYear() + "-" + d.getMonth() + "-" + d.getDate() + "T" + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds()).appendTo(div);
div.appendTo(container);
}
$('#submit').click(function () {
addComment($('#comments').val());
$('#comments').val("");
});
Upvotes: 1
Views: 144
Reputation: 30993
You are not building any HTML element, try to enclose your text in a div
instead.
Code:
var $fulaDate = $('<div>'+d.getFullYear() + "-" + d.getMonth() + "-" + d.getDate() + "T" + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds()+'</div>').appendTo(div);
Demo: http://jsfiddle.net/9jsjw/
If you want to display the month name use an array of month and get its description using the month index.
Code:
var monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var $fulaDate = $('<div>'+d.getFullYear() + "-" + monthNames[d.getMonth()] + "-" + d.getDate() + "T" + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds()+'</div>').appendTo(div);
Upvotes: 1
Reputation: 3966
replace those 3 lines with:
var d = new Date();
var $fulaDate = d.getFullYear() + "-" + d.getMonth() + "-" + d.getDate() + "T" + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
div.html($fulaDate);
Demo: http://jsfiddle.net/LCA72/6/
Upvotes: 0
Reputation: 4755
The problem is that you've got your date strings wrapped in the jQuery selector, so instead of being treated as a string, it's looking for an element that matches that string. Here's a fiddle that works, by saving that date info as a var, and then doing div.append($fullDate)
rather than using .appendTo(div)
.
There may be other bugs in your code, I didn't dig into anything beyond the question of it displaying... but it now displays the value as you're expecting.
Upvotes: 4