Subodh Joshi
Subodh Joshi

Reputation: 13482

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"

Upvotes: 1

Views: 2019

Answers (2)

Nikhil Maheshwari
Nikhil Maheshwari

Reputation: 2248

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/

Upvotes: 1

hityagi
hityagi

Reputation: 5256

Just use this :

var fullDate = new Date();
$('#date').text(fullDate);

$(function() {
  $("#Get").click(function() {
    var fullDate = new Date();
    $('#date').text(fullDate);
    $('#msg').text($('#comment').val());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<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" id="comment" />
</div>

<button id="Get">Get TextBox Value</button>

Upvotes: 0

Related Questions