Create Dynamic Web Pages With JavaScript
Learn how to use JavaScript to create dynamic and interactive web pages by manipulating the HTML and CSS elements using the DOM and selectors.
Generating dynamic and interactive web pages is possible with JavaScript, a popular and powerful programming language. JavaScript can manipulate the HTML and CSS elements on a web page, respond to user events, communicate with servers, and perform various tasks.
In this article, we will learn how to use JavaScript to create dynamic web pages with some simple examples.
What is JavaScript?
JavaScript is a server-side and browser-based scripting language. It was made by Brendan Eich in 1995, a Netscape engineer, as a way to add interactivity to web pages. JavaScript is not related to Java, despite the similar name.
- JavaScript is based on the ECMAScript standard, which defines the syntax and features of the language.
- JavaScript can also use various extensions and libraries, such as jQuery, React, Node.js, etc, to enhance its functionality and compatibility.
How to Use JavaScript in a Web Page?
There are two main ways to use JavaScript in a web page: inline and external. Inline JavaScript means writing JavaScript code directly in the HTML file using the <script> tag.
- External JavaScript means writing JavaScript code in a separate file using the
.jsextension, and linking it to the HTML file using thesrcattribute of the<script>tag.
Example:
<!-- Inline JavaScript -->
<html>
<head>
<script>
// Write your JavaScript code here
</script>
</head>
<body>
<!-- Your HTML content here -->
</body>
</html>HTML-generated code:
<!-- External JavaScript -->
<html>
<head>
<script src="script.js"></script>
</head>
<body>
<!-- Your HTML content here -->
</body>
</html>It is generally recommended to use external JavaScript, as it makes the code more organized, reusable, and maintainable. It also improves the performance and loading time of the web page, as the browser can cache the external JavaScript file.
How Can I Modify HTML and CSS Elements Using JavaScript??
One of the most common uses of JavaScript is to manipulate the HTML and CSS elements on a web page, such as changing the content, style, or position of an element.
- To do this, we need to use the Document Object Model (DOM), which is a representation of the web page as a tree of objects.
- Each object corresponds to an HTML element and has properties and methods that we can access and modify using JavaScript.
To access an HTML element using JavaScript, we need to use a selector, which is a way to identify the element based on its attributes, such as id, class, name, tag, etc. There are various methods to select an HTML element using JavaScript, such as:
document.getElementById(id): This method returns the element that has the specified id attribute.document.getElementsByClassName(class): This method returns a collection of elements that have the specified class attribute.document.getElementsByTagName(tag): This method returns a collection of elements that have the specified tag name, such as<p>,<div>, etc.document.querySelector(selector): This method returns the first element that matches the specified CSS selector, such as#id,.class,tag, etc.document.querySelectorAll(selector): This method returns a collection of elements that match the specified CSS selector.
Get Fatima Nawaz’s stories in your inbox
Join Medium for free to get updates from this writer.
Once an HTML element has been chosen, we can work with it using its methods and properties. For example, we can modify an element’s content using the innerHTML property or its style using the style property.
Here are some examples of how to use JavaScript to manipulate HTML and CSS elements:
HTML-generated code:
<!-- HTML file -->
<html>
<head>
<script src="script.js"></script>
</head>
<body>
<h1 id="title">Hello, World!</h1>
<p class="paragraph">This is a paragraph.</p>
<p class="paragraph">This is another paragraph.</p>
<button id="button">Click Me</button>
</body>
</html>JavaScript-generated code.
// JavaScript file
// Select the element with id "title"
var title = document.getElementById("title");// Change the content of the element
title.innerHTML = "Hello, JavaScript!";// Change the style of the element
title.style.color = "red";
title.style.fontSize = "36px";// Select the elements with class "paragraph"
var paragraphs = document.getElementsByClassName("paragraph");// Loop through the collection of elements
for (var i = 0; i < paragraphs.length; i++) {
// Change the content of each element
paragraphs[i].innerHTML = "This is a paragraph modified by JavaScript.";
}// Select the element with id "button"
var button = document.getElementById("button");// Add an event listener to the element
button.addEventListener("click", function() {
// This function will be executed when the button will clicked
// Change the content of the button
button.innerHTML = "You clicked me!";
// Change the style of the button
button.style.backgroundColor = "green";
});
Conclusion
Dynamic and interactive web pages can be made with JavaScript, a flexible and potent programming language. In this article, we learned how to use JavaScript to manipulate the HTML and CSS elements on a web page using the DOM and selectors.
There are many more things that JavaScript can do, such as making HTTP requests, storing data, creating animations, etc.
If you liked my content then Subscribe to my Newsletter and support me by Buying A Coffee.
Stackademic
Thank you for reading until the end. Before you go:
- Please consider clapping and following the writer! 👏
- Follow us on Twitter(X), LinkedIn, and YouTube.
- Visit Stackademic.com to find out more about how we are democratizing free programming education around the world.










