Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HTML
<h1>Javascript date function</h1>
  <p id="demo">This is a paragraph. </p>
 Display date button

What would be the function so that the paragraph would change into the date when you press the button at the bottom? Thanks.

What I have tried:

JavaScript
var d = new Date();
d.setFullYear(2020);
document.getElementById("demo").innerHTML = d;
Posted
Updated 2-Dec-20 1:52am
v2

1 solution

You need to define a JavaScript function (which will be called on button click) and access the paragraph to make any change to it.

Here, a working example:
HTML
<!DOCTYPE html>
<html>
<script>
function tryme()
{
  var d = new Date();
  d.setFullYear(2020);
  document.getElementById("demo").innerHTML = d;
}
</script>

<body>

<h1>The button Element</h1>
<p id="demo">This is a paragraph. </p>
<button type="button" onclick="tryme()">Click Me!</button>
 
</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900