1

I have a .js file with:

function date() 
{
    document.getElementById("id1").innerHTML = Date();
}

And a page with:

<p id="id1"></p>
<button type="button" onclick="date()">Click here</button>

But clicking on it doesn't do anything. What's missing?

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • You need to load the file using ` – zerkms Nov 04 '12 at 22:36
  • 1
  • 2
    @zerkms - it is a beginner question to be sure, but everyone is a beginner at some stage. Sometimes things like this are easy to miss when you're absorbing a lot of new information. Very valid question, and since Stackoverflow is a place that a lot people who are new to this stuff do come to to "read basics of js and html" it is a perfectly reasonable question. – Michael Shimmins Nov 04 '12 at 22:43
  • @Michael Shimmins: if someone doesn't know that to use js function you need to load a file - than they don't know literally anything. You can continuously ask trivial questions here, but if you want to learn something - you need to start reading from the very basics. – zerkms Nov 04 '12 at 22:51
  • @zerkms - agreed, but my point is you don't know the OP didn't do some research first. My point is that when you're reading a lot of new information, subtle things like an include tag can be easily missed, as you focus on what you perceive to be the important bits, (in this instance the content of the javascript). The other stuff looks all kinda stange and blends together. – Michael Shimmins Nov 04 '12 at 23:02
  • @Michael Shimmins: indeed, that's why I prepended my comment with "If you didn't know that" - to distinguish between "just have read and missed" and "never knew" – zerkms Nov 04 '12 at 23:03

2 Answers2

2

just add

<script type='text/javascript' src="path_to_js_file"></script>

on top of your html page

Michael Shimmins
  • 19,961
  • 7
  • 57
  • 90
Martin
  • 896
  • 1
  • 7
  • 22
0

I found a problem here:

First, you need to create a Date object, like so:

var d = new Date();

Then use getDate(), getMonth(), and so on...

var full = d.getMonth + 1; // getMonth returns 0 - 11
full += "/" + d.getDate;
full += "/" + d.getFullYear;
document.getElementById("id1").innerHtml = full;

Also, don't forget to include <script src="script.js"></script> in your head.

Andrew Sun
  • 270
  • 3
  • 11