-1

I know that it can be achieved by getElementById function also, but i want use getElementsByClassName function.

<!DOCTYPE html>
<html>
    <body>

        <h1>Date And Time</h1>

        <button type="button" onclick="document.getElementByClassName('demo').innerHTML=Date()">Click me to display Date and Time.</button>

        <p id="demo"></p>

    </body>
</html> 

I am biggner in Java, So please help me.... thank You

John Conde
  • 217,595
  • 99
  • 455
  • 496
gopal sharma
  • 129
  • 1
  • 6

2 Answers2

1

First of all this is javascript not java. Now your problem is you are accessing p tag with class name but that p tag doesn't have a class so,

Replace

<p id="demo"></p>

with

<p class="demo"></p>
Naqash Malik
  • 1,707
  • 1
  • 12
  • 14
  • may also be worth noting that `document.getElementByClassName` should be `document.getElementsByClassName` which returns an array so it will need to be `document.getElementsByClassName('demo')[0].innerHTML=Date()` – Martin Glennon-Brown May 06 '16 at 12:37
0

getElementsByClassName() returns a node list. [0] gets the first.

    <button type="button" onclick="document.getElementsByClassName('demo')[0].innerHTML='Hello'">Click me to display Date and Time.</button>

    <p class="demo"></p>
Felix
  • 65
  • 1
  • 7