Subodh Joshi Subodh Joshi - 5 months ago 8
HTML Question

Jquery: How to show date with textbox value?

I am newbie in jquery and i wrote below code

<h1>Type your comment below </h1>
<h2>TextBox value : <label id="msg"></label>-<label id="date"></label></h2>
<div style="padding:16px;">
TextBox : <input type="text" value="" placeholder="Type Your Comment"></input>
</div>
<button id="Get">Get TextBox Value</button>


var fullDate = new Date();
$("button").click(function(){
$('#msg').html($('input:text').val());
});


How to display the Date when submit button press for the label have
id="date"
?
When user press submit button i want to display

"User entered Textbox Value - Date with time"

Answer

Just use proper css selectors to add the messages and date. Refer code below :

 $("button").click(function() {
    var msg = $('input:text').val();
    var fullDate = new Date();

    $('#msg').html(msg);
    $('#date').html(toLocal(fullDate));
  });

 function toJSONLocal (date) {
   var local = new Date(date);
   local.setMinutes(date.getMinutes() - date.getTimezoneOffset());
   return local.toJSON().slice(0, 10);
 }

 function toLocal (date) {
   var local = new Date(date);
   local.setMinutes(date.getMinutes() - date.getTimezoneOffset());
   return local.toJSON().replace('T', ' ').slice(0, 19);
 }

Note: toJSONLocal, toLocal is used to format date.

jsfiddle : https://jsfiddle.net/nikdtu/q8zmLebz/