Interactive HTML with JavaScript Example Tutorial

In the dynamic world of web development, HTML and JavaScript are like peanut butter and jellyβ€”two seemingly simple components that, when combined, create something truly powerful. While HTML provides the structure and content for web pages, JavaScript brings them to life with interactivity and functionality. Here, we'll explore a comprehensive example that combines HTML and JavaScript, creating a simple to-do list application.

HTML input types
HTML input types

First, let's set up our basic HTML structure. We'll need a heading, an unordered list to hold our tasks, and a form to add new tasks. We'll also include a script tag at the end of the body where our JavaScript code will reside.

HTML,CSS JAVASCRIPT cheatsheet
HTML,CSS JAVASCRIPT cheatsheet

Setting up the HTML Structure

The core of our HTML structure is an unordered list (

    ) that will hold our tasks. Each task is represented by a list item (
  • ). We also include a form with an input field and a button to add new tasks.

Update HTML in DOM: Change Text Live
Update HTML in DOM: Change Text Live

Here's a simple example of what the HTML structure might look like:

```html

  • Task 1
  • Task 2
  • Task 3

login form html css
login form html css

```

Adding IDs and Classes for JavaScript Interaction

To make our HTML elements interactable with JavaScript, we've added IDs and classes. IDs are unique identifiers for elements, allowing us to target them directly. Classes, on the other hand, group elements together, enabling us to apply styles or behaviors to multiple elements at once.

In our example, we've added an ID 'taskList' to our unordered list and another ID 'addTaskForm' to our form. We've also added an ID 'newTask' to our input field. These IDs will be used to target these elements in our JavaScript code.

CrΓ©ation d'un site CV monopage responsive en HTML et CSS - Pierre Giraud
CrΓ©ation d'un site CV monopage responsive en HTML et CSS - Pierre Giraud

Introducing JavaScript for Functionality

Now that we have our HTML structure set up, let's introduce JavaScript to bring some functionality to our to-do list. We'll use JavaScript to add new tasks, remove them, and manage the list.

JavaScript can interact with HTML elements through the Document Object Model (DOM). The DOM represents the structure of the document and provides a way for programming languages to manipulate the content, structure, and style of the document.

JavaScript Full Notes Every Beginner Must Save πŸ’‘
JavaScript Full Notes Every Beginner Must Save πŸ’‘

Adding a New Task

To add a new task, we'll first get the value entered in the input field. Then, we'll create a new list item with that value and append it to our task list. Here's how we can do this:

JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
the cover of 52 html and css project ideas save this for later
the cover of 52 html and css project ideas save this for later
HTML Cheat Sheet πŸ“„βœ¨ Quick Guide for Beginners
HTML Cheat Sheet πŸ“„βœ¨ Quick Guide for Beginners
🏷️ HTML Attributes Explained | The Secret Behind Powerful HTML Tags
🏷️ HTML Attributes Explained | The Secret Behind Powerful HTML Tags
πŸ“˜ JavaScript Basics| Linking JS File to HTML | Chapter 2 πŸ“– Part 2
πŸ“˜ JavaScript Basics| Linking JS File to HTML | Chapter 2 πŸ“– Part 2
Modern Web Essentials Using JavaScript and HTML5
Modern Web Essentials Using JavaScript and HTML5
π™ƒπ™π™ˆπ™‡ π™π™–π™œπ™¨ π™˜π™π™šπ™–π™© π™¨π™π™šπ™šπ™©
π™ƒπ™π™ˆπ™‡ π™π™–π™œπ™¨ π™˜π™π™šπ™–π™© π™¨π™π™šπ™šπ™©
The JavaScript Cheat Sheet Every Beginner Wishes They Had
The JavaScript Cheat Sheet Every Beginner Wishes They Had
the different types of html tags are shown in this screenshote screen graber
the different types of html tags are shown in this screenshote screen graber
How To Create Rainbow Text Using CSS JS
How To Create Rainbow Text Using CSS JS
sunset evening using html css javascript
sunset evening using html css javascript
JavaScript this Keyword: Master Context Like a Pro πŸš€
JavaScript this Keyword: Master Context Like a Pro πŸš€
an image of a web page with different types of text
an image of a web page with different types of text
Create Image Accordion Slider With HTML CSS And JavaScript
Create Image Accordion Slider With HTML CSS And JavaScript
18 Exciting HTML and CSS Project Ideas with source code to Level Up Your Web Development Skills
18 Exciting HTML and CSS Project Ideas with source code to Level Up Your Web Development Skills
How To Create Autocomplete In Pure Javascript
How To Create Autocomplete In Pure Javascript
Your First Step Into Web Development
Your First Step Into Web Development
a handwritten diagram with the words html and an image of a computer screen on it
a handwritten diagram with the words html and an image of a computer screen on it
an info sheet with different types of web pages
an info sheet with different types of web pages
a black and orange poster with text that says,'html chat sheet'in different languages
a black and orange poster with text that says,'html chat sheet'in different languages

First, we'll add an event listener to our form that listens for the submit event. When the form is submitted, it prevents the default form submission behavior (which would refresh the page) and triggers our JavaScript code.

```javascript document.getElementById('addTaskForm').addEventListener('submit', function(event) { event.preventDefault(); // Prevent the default form submission behavior // Get the value entered in the input field var newTaskValue = document.getElementById('newTask').value; // Create a new list item var newTaskItem = document.createElement('li'); // Set the text of the new list item to the value entered by the user newTaskItem.textContent = newTaskValue; // Append the new list item to the task list document.getElementById('taskList').appendChild(newTaskItem); }); ```

Removing a Task

To remove a task, we'll add an event listener to each list item that listens for the click event. When a list item is clicked, it removes that item from the DOM. Here's how we can do this:

First, we'll select all the list items in our task list. Then, we'll loop through them and add an event listener to each one.

```javascript var taskItems = document.querySelectorAll('#taskList li'); taskItems.forEach(function(taskItem) { taskItem.addEventListener('click', function() { this.remove(); }); }); ```

And there you have itβ€”a simple to-do list application that demonstrates the power of combining HTML and JavaScript. With just a little bit of code, we've created an interactive web application that allows users to add and remove tasks.

Related Articles

Html To Zip Maker Microsoft Learn Html Responsive Web Meaning Responsive Html W3 Table Tag In Html Javatpoint Html With Link Example Html Links Tutorial Html Myanmar Is Tutorialspoint Legit Reddit Basic Html List