Exploring the World of Web Development Part 3 - Manipulating HTML Using Javascript
Basically an HTML is the face of any web page, it is very static, meaning it doesn’t know how to handle user input, it doesn’t have enough facility to manipulate the interface of a web page. You can only use something like Hyperlink to navigate from one page to another. HTML can not store any user input data without using another help, to help HTML manipulating a web page data or interface we use Javascript.
JavaScript allows you to dynamically change the content and styling of your web pages. JavaScript is a powerful tool that can be used to change Document Object Model (DOM). DOM is a representation of the HTML structure of a web page. For example “<div>The Content</div>” is an example of DOM, basically any part of HTML is A DOM.
What a JavaScript codes can do to your HTML pages:
Web Page Dynamic content / DOM manipulation
JavaScript capability can make any update to the content of HTML elements dynamically. For example, you can append, remove or alter the text inside a <div> tag using something like:
Event handling
When a user creates an interaction to your web page, such as clicking a button, moving a mouse, or typing on the keyboard, Javascript can listen to that interaction and you can make codes to handle that and decide what will be the action of such behavior. For example, when a user clicking on a button, you can display a message like:
Form validation
When you want to create an interactive website, you have to validate any of Form submission from the user in the Front end, not just back-end validation. For example you have to check if Contact Form required fields like Name, Email and Body are filled, with the correct format and minimum/maximum length.
Attribute Change or Styling
Any HTML tags accept any CSS styling using the Style attribute, for example <div style=”color:red”>. You can do it too with JavaScript, helping you make styling on your page more dynamically. In fact are not just limited to change the style but also can change any properties of your HTML elements.
HTTP Request AJAX
When you want to add data dynamically to your Page, you can use JavaScript to make Ajax Requests. You can make any type of request either GET or POST. The data request is done asynchronously, meaning it separates the process from rendering the initial view, making your page smoother and interactive. Also the requests to a server can be triggered anytime with Javascript without the need of reloading the entire web page.
You have explored how JavaScript can be used to build an interactive web app. From form validation, and manipulating the DOM. Below is a full provided example I post on Github, you can see the code examples of implementing form validation using vanilla JavaScript and integrating libraries like Bootstrap, and styling the web page using CSS.
Here’s the full example of a Javascript Fetch and Form Validation, there’s two HTML: index.html and contact.html. You can open it straight to your browser to check its functionality, and use VS-Code to read and tinker with each part of the code.
Thank you for joining me on this web development tutorial. We already learn front end technology to develop a web app, we have learned about HTML, CSS and JavaScript. The journey is fun and I hope you enjoy and read it all from start to the end. I hope the last three tutorials have provided valuable insights and inspiration for you to begin Front end web development using HTML, CSS and JavaScript. There will be more in-depth coding and explanation in the next article, keep following this blog, Happy coding!
Here’s the previous tutorial about css:
- Part 2: Styling your web page with CSS
- Part 4 : Develop Rest API with Spring Boot