teach-ict.com logo

THE education site for computer science and ICT

Question 20

There is a javascript command that can alter the text within a targetted id and it looks like this

                  document.getElementById('target').innerHTML

which will alter the text within this <p> pair:

<p id="target"></p>

There is also a built-in date function called Date() that returns the date.

 

 

Task: Starting from this, write a complete web page to include a button, that when clicked will display the date.

 


Answer:

The answer is shown below:

A <button> item is declared which shows a standard grey button and

It contains the label 'Click me ....'.

An onclick event is included that calls the standard document.getElementById javascript function.

The Date() function is used to feed this function with the current date.

This is then displayed when you click on it.

This is what the web page looks like before the button is clicked

And this is what it looks like when clicked