imulsion
imulsion

Reputation: 9040

JavaScript with HTML DOM doesn't work

My JavaScript function date(id) is supposed to display the current date by accessing an element passed in the function call. The problem is it doesn't work and I can't understand why. Can anyone help me?

Here is my code:

<script type = "text/javascript">
function date(id)
{
document.getElementById(id).innerHTML = Date();
}
</script>

in the HTML:

<p style = "text-align:center;" id = "datep">
</p>

<input type = "button" onclick = "date(datep)" value = "Display date" />

Upvotes: 1

Views: 434

Answers (1)

ZombieCode
ZombieCode

Reputation: 1661

In your input tag you have the id passing to your function. Your id is a string and since it is you need to put quotes around it.

It should look like this...

<input type="button" onclick="date('datep')" value="Display date" />

Upvotes: 2

Related Questions