3

I want to print the value of the date by passing it from the JavaScript file to the HTML file.

HTML file:

  <html> 
      <body> 
          <script src=background.js> </script> 
          <span id="showDate"> </span> 
      </body> 
  </html>

JavaScript file:

document.body.style.background = "url('1.jpg')";
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
  dd='0'+dd
} 
if(mm<10) {
  mm='0'+mm
} 
today = dd+'/'+mm+'/'+yyyy;
alert(today);
document.getElementById("showDate").value=today;
Maximillian Laumeister
  • 19,884
  • 8
  • 59
  • 78
  • possible duplicate of [How do I display a date/time in the user's locale format and time offset?](http://stackoverflow.com/questions/85116/how-do-i-display-a-date-time-in-the-users-locale-format-and-time-offset) – MaxZoom Aug 27 '15 at 17:45
  • use `textContent ` or `innerHTML` instead of `value` in last line of your code – J Santosh Aug 27 '15 at 17:54
  • This has nothing to do with chrome extensions. – Teepeemm Aug 27 '15 at 18:20

4 Answers4

0

Easiest way:

 document.getElementById("showDate").textContent = today;
Oskar Eriksson
  • 2,591
  • 18
  • 32
0

You just need to use textContent instead of value.

Live Working Demo:

document.body.style.background = "url('1.jpg')";
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) {
    dd = '0' + dd
}
if (mm < 10) {
    mm = '0' + mm
}
today = dd + '/' + mm + '/' + yyyy;
document.getElementById("showDate").textContent = today;
<span id="showDate"> </span> 

JSFiddle Version: https://jsfiddle.net/ae6wfz12/

Maximillian Laumeister
  • 19,884
  • 8
  • 59
  • 78
0

First of all link up your javascript file like into HTML file bottom part.

<script src="your_javascript_file.js"></script>

and then

Write Your js code into your js file with flowing line and pass inner html into

document.getElementById("showDate").innerHTML = today;
Jakir Hossain
  • 2,457
  • 18
  • 23
0

First of all, you need to put the scripts that access document elements at the end of the body tag, so that they are executed only after the required elements are loaded.

 <html> 
      <body> 
          <span id="showDate"> </span> 
      </body> 
      <script src=background.js> </script>
  </html>

Secondly, as Maximillian told, use textConten or innerHTML or innerText property of the selected element to get or set the value.

document.getElementById("showDate").innerHTML = '<strong>' + today '</strong>';

Prakash GPz
  • 1,675
  • 4
  • 16
  • 27