Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
Insert javascript method inside html paragraph
<!DOCTYPE html>
<html>
<body>

<h2>My First JavaScript</h2>

<p id="demo"><script>Date()</script></p>    <!-- here I want to put the output of Date() method -->

</body>
</html> 

I know that I can do something like:

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

But I'd like to know if there's a way to do that inside the paragraph directly.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use document.write to write content to the HTML directly from JavaScript.

<h2>My First JavaScript</h2>

<p id="demo"><script>document.write( Date())</script></p>

NOTE Calling document.write() after the initial HTML has been parsed will cause the entire HTML to be discarded and a new document will be created. This is only safe to be called while the document is being parsed.


Example of calling document.write after document has been processed

setTimeout(() => {
  document.write("Written after HTML doc was closed");
}, 5000);
<h1> Hello </h1>
<p> I will disappear after document.write is called</p>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!