Click here to Skip to main content
14,735,364 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<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:

var d = new Date();
d.setFullYear(2020);
document.getElementById("demo").innerHTML = d;
Posted
Updated 2-Dec-20 2: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:
<!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>
   

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