user2640633
user2640633

Reputation:

Insert variable into .append html

I am trying to insert the date and time (in a manner in which i have gotten to work in console.log). Essentially I have a forum and when the user comments on a post and clicks comment button it uploads that text he or she entered into the comment section. That works fine, BUT I want it to insert the current date and time too. As of now I just have a static date and time.

I tried using: document.write(dateNTime)

var t = new Date()

var DateNTime = (t.toDateString() + ", at " + t.getHours() + ":" + 
t.getMinutes())
console.log(DateNTime)

$('#commentButton').on('click', function() {
if($('#forumCommentSearchBox').val() ) {

  $('.commentList').append('<li> <div class="commenterImage"> \
                            <img 
src="http://lorempixel.com/50/50/people/9"> \
                            </div> \
                            <div class="commentText"> \
                              <p \ 
class="">'+$('#forumCommentSearchBox').val()+'</p> \
                              <span class="date sub-text">on March 5th, 
2014</span> \
                            </div></li>');


}
 else{
  alert("idiot");
}
});

Upvotes: 0

Views: 32

Answers (1)

Madness
Madness

Reputation: 2726

If I understand you correctly...

var t = new Date()

var DateNTime = (t.toDateString() + ", at " + t.getHours() + ":" + 
t.getMinutes())

$('#commentButton').on('click', function() {
if($('#forumCommentSearchBox').val() ) {

$('.commentList').append('<li> <div class="commenterImage"> \
                            <img src="http://lorempixel.com/50/50/people/9"> \
                            </div> \
                            <div class="commentText"> \
                              <p class="">'+$('#forumCommentSearchBox').val()+'</p> \
                              <span class="date sub-text">on ' + DateNTime + '</span> \
                            </div></li>');

}
 else{
  alert("idiot");
}
});

Upvotes: 0

Related Questions