Procedure
Step 1: Create the HTML File
Create a new file in a text editor and save it with a .html extension (e.g., index.html).
Then add an heading tag 'h1' with the text "Codedamn Lab" and set the initial font size of 16 pixels using 'style' attribute.
Now, create a button element with the text "Toggle Font Size" and give id as "toggleButton". After that,
link the script.js file externally using the 'script' tag.
Step 2: Create the JavaScript File
Create a new file in the same directory and save it with a .js extension (e.g., script.js).
This JavaScript file adds functionality to the webpage.
It selects the HTML elements with the ids "toggleButton" and "header" using document.getElementById.
An event listener is attached to the "toggleButton" element, listening for a 'click' event.
When the button is clicked, it checks the current font size of the "header" element.
If the font size is 16 pixels, it sets it to 32 pixels; otherwise, it sets it back to 16 pixels.
Step 3: Test the Font Size Toggle
Open the webpage in your browser.
Click the "Toggle Font Size" button to observe the interactive font size toggle in action.
Html
Css
Js
Explanation
The provided code consists of HTML and Javascript codes. It creates a webpage with a heading ("Codedamn Lab") and a button. When you click the button, it toggles the font size of the heading between 16 pixels and 32 pixels. It uses JavaScript to dynamically change the style of the heading element in response to button clicks.
In the HTML part:
There is an 'h1' tag with the text "Codedamn Lab," and it has an id attribute set to 'header'. Initially, its font size is set to 16 pixels.
Below the 'h1' tag, there is a 'button' element with the text "Toggle Font Size" and it has an id attribute set to 'toggleButton'.
There is an external JavaScript file linked using the 'script' tag with a src attribute pointing to "script.js." This file is where the JavaScript code is located.
In the JS part (inside script.js):
The JavaScript code is responsible for handling the functionality of toggling the font size of the 'h1' tag when the button is clicked. It uses the document.getElementById method to get references to the 'toggleButton' and 'header' elements.
An event listener is added to the 'toggleButton' element, which listens for a 'click' event. When the button is clicked, the code checks the current font size of the 'header' element. If the font size is 16 pixels, it sets it to 32 pixels, and if it's already 32 pixels, it sets it back to 16 pixels.