Javascript Display Examples
Javascript Display Examples
This document explains how to display JavaScript on a webpage using different methods. It includes inline
JavaScript, internal JavaScript, external JavaScript, and how to output/display results.
Example:
Example:
<!DOCTYPE html>
<html>
<head>
<script>
function showMessage() {
alert("Hello from internal JavaScript!");
}
</script>
</head>
<body>
Example:
<!DOCTYPE html>
<html>
<head>
<script src="script.js"></script>
</head>
<body>
<button onclick="sayHi()">Click Me</button>
</body>
</html>
This links the external JavaScript file.
script.js:
function sayHi() {
alert("Hello from external JS file!");
}
This file contains the JavaScript function.
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
alert("This is an alert box!");
console.log("This is logged in the console.");
document.write("This was written with document.write.<br>");
document.getElementById("demo").innerHTML = "This text was added to a paragraph.";
</script>
</body>
</html>
This example demonstrates all four methods of displaying output.
Skip to content
Open sidebar
<!DOCTYPE html>
<html>
<body>
<p>In this case JavaScript changes the value of the src (source) attribute of an image.</p>
</body>
</html>
There are three main ways to add JavaScript to your HTML file:
You can write JavaScript in the <head> or at the bottom of <body> using the <script> tag:
<!DOCTYPE html>
<html>
<head>
<script>
function turnOn() {
document.getElementById("myImage").src = "pic_bulbon.gif";
function turnOff() {
document.getElementById("myImage").src = "pic_bulboff.gif";
</script>
</head>
<body>
<br><br>
</body>
</html>
function turnOn() {
document.getElementById("myImage").src = "pic_bulbon.gif";
function turnOff() {
document.getElementById("myImage").src = "pic_bulboff.gif";
index.html
<!DOCTYPE html>
<html>
<head>
<title>External JS Example</title>
<script src="script.js"></script>
</head>
<body>
<br><br>
</body>
</html>
<p id="demo">Hello</p>
<button onclick="document.getElementById('demo3').style.display='none'">Hide</button>
<button onclick="document.getElementById('demo3').style.display='block'">Show</button>
Your code is using inline JavaScript, where the JavaScript is directly inside the onclick attribute. That’s
perfectly okay for small examples but for better organization and readability, <script> is preferred.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
body {
text-align: center;
background-color: #f4f4f4;
padding-top: 50px;
h1 {
color: #333;
#myImage {
width: 120px;
margin: 20px;
border-radius: 10px;
button {
margin: 10px;
font-size: 16px;
border: none;
border-radius: 5px;
background-color: #007bff;
color: white;
cursor: pointer;
button:hover {
background-color: #0056b3;
#statusText {
font-size: 18px;
margin-top: 20px;
</style>
</head>
<body>
<div>
</div>
<script>
function turnOn() {
document.getElementById("myImage").src = "pic_bulbon.gif";
function turnOff() {
document.getElementById("myImage").src = "pic_bulboff.gif";
</script>
</body>
</html>
✅ HTML Section
id="myImage": Gives the image an ID so JavaScript can find and change it.
CSS makes the page look nice: colors, spacing, fonts, hover effects.
function turnOn() {
document.getElementById("myImage").src = "pic_bulbon.gif";
🔸 3. How to Run
Would you like a second mini-project like a calculator, clock, or color changer to practice further?
You said:
Yes continue
ChatGPT said:
Awesome! Let’s continue with a second mini-project that’s simple, visual, and fun to practice with. We’ll make
a "Color Changer Box" where clicking a button changes the background color randomly.
✅ Project 2: Random Background Color Changer
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
body {
text-align: center;
margin-top: 50px;
background-color: #f0f0f0;
#colorBox {
width: 300px;
height: 200px;
border-radius: 10px;
background-color: white;
display: flex;
align-items: center;
justify-content: center;
font-size: 20px;
font-weight: bold;
button {
font-size: 16px;
border: none;
border-radius: 5px;
background-color: #28a745;
color: white;
cursor: pointer;
button:hover {
background-color: #218838;
</style>
</head>
<body>
<script>
function changeColor() {
document.getElementById('colorBox').style.backgroundColor = randomColor;
document.getElementById('colorBox').innerHTML = randomColor.toUpperCase();
</script>
</body>
</html>
🔸 2. Explanation
✅ HTML
✅ JavaScript
document.getElementById('colorBox').style.backgroundColor = randomColor;
document.getElementById('colorBox').innerHTML = randomColor.toUpperCase();
Click the button and watch the color (and color code) change.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
body {
background-color: #222;
color: #fff;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
flex-direction: column;
h1 {
margin-bottom: 20px;
color: #00ffcc;
#clock {
font-size: 60px;
background-color: #333;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0,255,204,0.5);
letter-spacing: 4px;
</style>
</head>
<body>
<div id="clock">00:00:00</div>
<script>
function updateClock() {
// Display it
document.getElementById("clock").innerHTML = timeString;
setInterval(updateClock, 1000);
// Run once immediately
updateClock();
</script>
</body>
</html>
✅ HTML
<div id="clock">00:00:00</div>
✅ CSS
#clock {
font-size: 60px;
background-color: #333;
padding: 20px;
✅ JavaScript
setInterval(updateClock, 1000);
<!DOCTYPE html>
<html>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
</body>
</html>
✅ Explanation:
✅ Good for:
Simple learning
<!DOCTYPE html>
<html>
<body>
<button onclick="document.getElementById('bulb').src='pic_bulbon.gif'">TurnON</button>
<buttononclick="document.getElementById('bulb').src='pic_bulboff.gif'">TurnOFF</button>
</body>
</html>
Explanation:
The inline JavaScript inside the onclick attribute changes the image’s src.
Note: You must have pic_bulboff.gif and pic_bulbon.gif in your project folder.
Let’s create a form that checks if the name input is empty when you click "Submit".
<html>
<body>
</form>
<script>
function validateForm() {
</script>
</body>
</html>
Explanation:
Form has onsubmit="return validateForm()" — this calls the validateForm() JavaScript function.
Example:
When the button is clicked, the JavaScript inside onclick runs and shows an alert box.
You write JavaScript inside a <script> block, usually in the <head> or at the end of the <body>.
Example:
<!DOCTYPE html>
<html>
<head>
<script>
function showMessage() {
</script>
</head>
<body>
</body>
</html>
You save your JavaScript code in a separate .js file and link it using the <script src="..."></script> tag.
index.html
<!DOCTYPE html>
<html>
<head>
<script src="script.js"></script>
</head>
<body>
</body>
</html>
script.js
function sayHi() {
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
</script>
</body>
</html>
Would you like a practice file to try all of these? Or shall I guide you in setting up your own .js file and
connecting it to HTML?
You said:
Please write this flle in docx file format . I need to download and edit without reduse with Formatting
ChatGPT said:
I've created the document with all the examples and explanations you requested. You can download it using
the link below:
Feel free to edit it as needed! Let me know if you need any further assistance.
No file chosenNo file chosen