SA-21/06, C-11, Asapur, Varanasi, UP support@kamaldnp.com +91 8318013442
HTML
1. HTML Introduction: What is HTML? Learn with Examples 2. Best Free HTML Editors: Notepad to VS Code for Beginners 3. HTML Basic Structure: Doctype, Head, Body Explained 4. HTML Elements Explained: Tags, Content & Empty Elements 5. HTML Attributes List: href, src, alt, width, title & More 6. HTML Headings h1 to h6: Which Heading Tag to Use When 7. HTML Paragraphs: p Tag, br Line Breaks & pre Text 8. HTML Style Attribute: Colour, Font Size & Alignment 9. HTML Text Formatting Tags: bold, italic, mark & More 10. HTML Comments: How to Add Notes & Hide Code in HTML 11. HTML Colors: Names, HEX, RGB & HSL Colour Codes 12. HTML CSS: Inline, Internal & External Stylesheets 13. HTML Links: a Tag, href, New Tab & Email Links 14. HTML Images: img Tag, src, alt, Width & Formats 15. HTML Favicon: Create & Add a Browser Tab Icon 16. HTML Title Tag: How to Write a Good Page Title 17. HTML Tables: table, tr, th, td, colspan & rowspan 18. HTML Lists: ul, ol, dl & Nested Lists Explained 19. HTML Block vs Inline Elements: div, p, span Explained 20. HTML Div Tag: Group Content & Build Page Layouts 21. HTML Class Attribute: Style Many Elements at Once 22. HTML id Attribute: Unique IDs, Links, CSS & JavaScript 23. HTML Iframes: Embed YouTube, Maps & Pages in HTML 24. HTML JavaScript: script Tag, Clicks & First Example 25. HTML File Paths: Relative vs Absolute Paths Explained 26. HTML Head Element: title, meta, charset & viewport 27. HTML Layout: header, nav, main, footer Page Structure 28. HTML Responsive Design: Viewport, Images & Media Queries 29. Semantic HTML Tags: header, nav, main, article & footer 30. HTML Forms: input, label, select, checkbox & Submit 31. HTML Entities: & < > © with Examples 32. HTML Audio & Video Tags: controls, source & Fallback 33. HTML Accessibility: alt Text, Labels, Landmarks & ARIA 34. HTML Meta Tags & SEO: charset, viewport, OG Tags 35. HTML Quotations: blockquote, q, cite, abbr & address
Final Test — Certificate ₹10
Try this code live
Lesson 24 of 35

HTML JavaScript: script Tag, Clicks & First Example

3 min read · Free · No login needed

JavaScript is the programming language of the web. HTML builds the page, CSS styles it, and JavaScript makes it do things: respond to clicks, check a form before it is sent, show and hide content, load new data without refreshing the page.

Diagram — see how it fits together
Infographic: HTML plus JavaScript

Adding JavaScript to a page

Write it inside a <script> tag, usually just before the closing </body> so the page content loads first. Or keep it in a separate .js file and link it: <script src="app.js"></script>. The separate-file way is better for real sites — one script file can serve every page.

What JavaScript can do

  • Change content: document.getElementById("demo").innerHTML = "Hello!"; replaces the text inside an element.
  • Change styles: element.style.color = "red"; recolours it.
  • React to events: run code when a button is clicked, a form is submitted, or a key is pressed.
  • Check input: make sure an email looks like an email before the form is sent.

Your first script

The example below has a button. When you click it, JavaScript changes the text of a paragraph. It is only three lines — but it is the same idea behind every interactive feature on the web, from a "like" button to a full quiz timer.

Tip: Open your browser's developer tools (F12) and click the Console tab. Any error in your JavaScript appears there, with the line number. It is the first place to look when a script does not work.

What JavaScript is

JavaScript is the programming language of the web. HTML builds the page, CSS paints it, and JavaScript makes it do things — respond to clicks, check a form before it is sent, load new data without reloading the page. It runs in the browser, on the visitor's computer, and every modern website uses it.

Syntax

JavaScript goes inside a <script> tag. Put the tag at the end of the <body>, so the HTML loads first and the script can find the elements it needs:

<p id="demo">Hello.</p>
<script>
document.getElementById("demo").innerHTML = "Hello, JavaScript!";
</script>

This short script finds the paragraph with id="demo" and replaces its text. When the page loads, the visitor sees "Hello, JavaScript!" instead of "Hello."

Note: A <script> in the <head> runs before the page body exists, so it cannot find your elements yet. That is why scripts usually go at the end of the body — or use the defer attribute, which tells the browser to wait until the page is ready.
Tip: Press F12 in Chrome to open DevTools, then click the Console tab. You can type JavaScript there and see the result instantly. It is the fastest way to experiment — try document.title and press Enter.

Common mistakes

  • Putting the script before the elements it uses — the script runs, finds nothing, and silently does nothing.
  • Forgetting a closing </script> — the rest of the page is then treated as script and disappears.
  • Mixing JavaScript into HTML attributes everywhere — for anything bigger than one line, use a separate script block or file.
  • Assuming JavaScript is always on — a few users switch it off, so the page should still make sense without it.

Example explained

  • <p id="demo"> — the paragraph JavaScript will change. The id gives it a name the script can find.
  • <button onclick="..."> — the onclick attribute says "when this button is clicked, run this JavaScript".
  • document.getElementById('demo') — finds the paragraph by its id, like looking someone up by name.
  • .innerHTML = 'Hello, JavaScript!' — replaces the text inside the paragraph with the new message, instantly, without reloading the page.

Example

Example
<!DOCTYPE html>
<html>
<head>
<title>JavaScript</title>
</head>
<body>

<p id="demo">Click the button to change this text.</p>
<button onclick="document.getElementById('demo').innerHTML = 'Hello, JavaScript!'">Click Me</button>

</body>
</html>

Output

Output — how it looks in the browser
Output of HTML JavaScript: script Tag, Clicks & First Example

Try it yourself — live editor

Change the code below and press Run to see the result instantly. No file to save, no login needed.

Result

Frequently asked questions

The questions learners ask most about this lesson — read the question, then open it to see the answer.

1. What is JavaScript?

It is the programming language of the web; HTML builds the page, CSS styles it, and JavaScript makes it do things like respond to clicks.

2. Where should you put a script tag?

At the end of the body, so the HTML loads first and the script can find the elements it needs.

3. What happens if a script runs before its elements exist?

It finds nothing and silently does nothing, so scripts usually go at the end of the body or use the defer attribute.

Why learn this yourself — and not just ask AI?

AI can write code for you, but it cannot think for you. When you learn HTML yourself, your brain builds the real skill: you understand why a page looks the way it does, you can fix it when it breaks, and you can answer it in an exam or an interview without help. That is the skill that gets you a job — not a copied answer.

Get certified — just ₹10

Finish all 35 lessons, pass the test (80%), pay ₹10 and get your certificate.

Take the test

Prefer watching? Video courses teach the same topics step by step.

Go further with a full course

Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.

Core Python Programming – 3 Month Complete Course
₹700
View
AI Power Skills in 2 Days | Learn AI Text, Image, Video and Voice Tools
View
Full Stack PHP Development Course 2026
₹3,000
View
Browse all courses
HTML — Lessons
1. HTML Introduction: What is HTML? Learn with Examples 2. Best Free HTML Editors: Notepad to VS Code for Beginners 3. HTML Basic Structure: Doctype, Head, Body Explained 4. HTML Elements Explained: Tags, Content & Empty Elements 5. HTML Attributes List: href, src, alt, width, title & More 6. HTML Headings h1 to h6: Which Heading Tag to Use When 7. HTML Paragraphs: p Tag, br Line Breaks & pre Text 8. HTML Style Attribute: Colour, Font Size & Alignment 9. HTML Text Formatting Tags: bold, italic, mark & More 10. HTML Comments: How to Add Notes & Hide Code in HTML 11. HTML Colors: Names, HEX, RGB & HSL Colour Codes 12. HTML CSS: Inline, Internal & External Stylesheets 13. HTML Links: a Tag, href, New Tab & Email Links 14. HTML Images: img Tag, src, alt, Width & Formats 15. HTML Favicon: Create & Add a Browser Tab Icon 16. HTML Title Tag: How to Write a Good Page Title 17. HTML Tables: table, tr, th, td, colspan & rowspan 18. HTML Lists: ul, ol, dl & Nested Lists Explained 19. HTML Block vs Inline Elements: div, p, span Explained 20. HTML Div Tag: Group Content & Build Page Layouts 21. HTML Class Attribute: Style Many Elements at Once 22. HTML id Attribute: Unique IDs, Links, CSS & JavaScript 23. HTML Iframes: Embed YouTube, Maps & Pages in HTML 24. HTML JavaScript: script Tag, Clicks & First Example 25. HTML File Paths: Relative vs Absolute Paths Explained 26. HTML Head Element: title, meta, charset & viewport 27. HTML Layout: header, nav, main, footer Page Structure 28. HTML Responsive Design: Viewport, Images & Media Queries 29. Semantic HTML Tags: header, nav, main, article & footer 30. HTML Forms: input, label, select, checkbox & Submit 31. HTML Entities: & < > © with Examples 32. HTML Audio & Video Tags: controls, source & Fallback 33. HTML Accessibility: alt Text, Labels, Landmarks & ARIA 34. HTML Meta Tags & SEO: charset, viewport, OG Tags 35. HTML Quotations: blockquote, q, cite, abbr & address
Final Test — Certificate ₹10

🤖 KAMALDNP AI Assistant

Ask about courses, website & more

Hi! I'm KAMALDNP AI Assistant. Ask me about our courses, website features, or anything else! 😊
Please wait 5s...
🤖 KAMALDNP Assistant ✕

Request human callback:

🔐 Login Required
✕

Please log in to download. You will stay on this page after logging in!

New here? Register now →