In this lesson, we will cover two major ideas that all developers should be familiar with if they want to build beautiful websites.
At the end of this lesson, students will have created a working program that enables them to manipulate strings, as well as provide feedback when a user clicks a button or hovers over a button. Let’s jump in!
To get started, let us build the basic HTML structure for the application.
<title>DAY-7</title>
<h2>JS String Method Example</h2>
<p id="demo"></p>
<h2>JavaScript Event Demo</h2>
<button>Click Me</button>
<div id="hoverBox" style="width: 150px;height: 150px;background-color: lightblue;text-align: center;line-height: 150px;font-size: 18px;cursor: pointer">
Hover Me
</div>
<p id="output"></p>
What’s happening here?
Pro Tip: Always give meaningful IDs to elements you want to manipulate with JavaScript. This makes your code easier to read and maintain!
Now let’s add JavaScript code to demonstrate powerful string methods.
let str = "Hello JavaScript World!";
let result = str.trim();
result = result.toUpperCase();
result = result.replace("JAVASCRIPT", ".NET ");
let part = result.slice(6, 12);
document.getElementById("demo").innerHTML =
"Modified: " + result + "<br>" +
"Part: " + part + "<br>" +
"Length: " + result.length;
Breaking it down:
Note: String methods DON’T change the original string – they return a new modified string! That’s why we store results in variables.
Create a new file called Index.js (or add this script in your HTML) with the following functions:
function showMessage() {
document.getElementById("output").innerHTML = "Button was clicked!";
}
What does this do?
When the button is clicked, this function:
This illustrates the onclick event, which is one of the most popular events in web development.
Common Use Cases: Submitting forms, adding items to a cart, opening modals, playing sounds, etc.
Add the following two functions to process mouse hover events:
function changeColor() {
document.getElementById("hoverBox").style.backgroundColor = "orange";
document.getElementById("hoverBox").innerHTML = "Mouse Over!";
}
function resetColor() {
document.getElementById("hoverBox").style.backgroundColor = "lightblue";
document.getElementById("hoverBox").innerHTML = "Hover Me";
}
How it works:
changeColor() –
resetColor() –
Pro Tip: CSS properties can be accessed in JavaScript with element.style.propertyName. Only to keep in mind to use camelCase (backgroundColor rather than background-color).
Remember to include your JavaScript file prior to the closing </body> tag:
Note: Scripts should be placed at the bottom of the body to ensure that all the HTML elements are loaded before JavaScript attempts to access them!
String Manipulation Section
Let’s understand the string methods in detail:
let str = "Hello JavaScript World!";
let result = str.trim(); // Step 1: Remove whitespace
result = result.toUpperCase(); // Step 2: Convert to uppercase
result = result.replace("JAVASCRIPT", ".NET "); // Step 3: Replace text
let part = result.slice(6, 12); // Step 4: Extract substring
Flow of transformation:
Why use these methods?
Event Handling Section
<button>Click Me</button>
Event Binding Explained:
The onclick attribute directly attaches the showMessage() function to the click event of the button.
When a user clicks:
Alternative Modern Approach:
// In your JavaScript file
document.getElementById("myButton").addEventListener("click", showMessage);
This is the new, desirable method because it separates HTML structure and JavaScript behavior!
DOM Manipulation Section
document.getElementById("hoverBox").style.backgroundColor = "orange";
What’s happening:
Key Concept: This is referred to as DOM Manipulation – dynamically manipulating HTML/CSS with JavaScript!
1. Components / Keywords Used
Name | Description |
let | Declares a block-scoped variable that can be reassigned |
document | Object representing the HTML document; entry point to the DOM |
getElementById() | Finds and returns an HTML element by its ID attribute |
innerHTML | Property to get or set the HTML content inside an element |
onclick | Event attribute that triggers when an element is clicked |
onmouseover | Event attribute that triggers when mouse pointer enters an element |
onmouseout | Event attribute that triggers when mouse pointer leaves an element |
style | Property to access and modify CSS styles of an element |
<script> | HTML tag used to embed or reference JavaScript code |
2. Key Properties / String Methods / Features
Name | Description |
trim() | Removes whitespace from both ends of a string |
toUpperCase() | Converts all characters in a string to uppercase letters |
toLowerCase() | Converts all characters in a string to lowercase letters |
replace(old, new) | Replaces the first occurrence of a substring with a new value |
slice(start, end) | Extracts a section of a string from start index to (end-1) index |
substring(start, end) | Similar to slice but doesn’t accept negative indices |
length | Property that returns the number of characters in a string |
backgroundColor | CSS property accessed via JavaScript to change background color |
cursor | CSS property that changes the mouse cursor appearance |
The following are some of the best sources to further your knowledge:
String Methods:
Events & DOM Manipulation:
Practice Platforms:
Interactive Learning:
Day 7 introduces three important concepts that make JavaScript interactive and powerful—Events, Strings, and String Methods. You learn how JavaScript events respond to user actions such as clicks, typing, hovering, and form submissions using event listeners. Then you explore strings, which represent text data, and understand how to create, combine, and format them efficiently. Finally, you study commonly used string methods like length, toLowerCase(), toUpperCase(), trim(), split(), replace(), and includes() to manipulate text easily. By the end of this lesson, you can build interactive web pages and control text dynamically with confidence.
Events are actions triggered by users such as clicking a button, typing in an input field, moving the mouse, or submitting a form.
An event listener waits for an event to occur and then runs a function when that event happens.
A string is a sequence of characters used to store text, such as names, messages, and user input.
String methods allow you to modify, search, format, and clean text data easily without writing complex logic.
No, Strings are immutable in JavaScript, which means methods like toUpperCase() or replace() always return a new string without changing the original one.
Copyright © 2025 Niotechone Software Solution Pvt. Ltd. All Rights Reserved.