How does inline JavaScript work with HTML ? Last Updated : 10 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Inline JavaScript refers to JavaScript code embedded directly within HTML elements using the onclick, onmouseover, or other event attributes. This allows you to execute JavaScript code in response to user interactions or specific events without needing a separate JavaScript file or script block.Syntax<script> // JavaScript Code</script>Example: In this example, the Inline JavaScript in the onclick attribute triggers the showAlert() function when the button is clicked, validating the input field and showing an alert with a greeting or error message. HTML <!DOCTYPE html> <html> <head> <title>Inline JavaScript</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href= "https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> </head> <body> <div class="container"> <h1 style="text-align:center;color:green;"> GeeksforGeeks </h1> <form> <div class="form-group"> <label for="">Enter Your Name:</label> <input id="name" class="form-control" type="text" placeholder="Input Your Name Here"> </div> <div class="form-group"> <button class="btn btn-success btn-lg float-right" type="button" onclick="showAlert()"> Submit </button> </div> </form> </div> <script> function showAlert() { let user_name = document.getElementById("name"); let value = user_name.value.trim(); if (!value) alert("Name Cannot be empty!"); else alert("Hello, " + value + "!\nGreetings From GeeksforGeeks."); } </script> </body> </html> Output:OutputFor deeper knowledge, you can visit What is the inline function in JavaScript?Note:Using inline JavaScript is generally considered bad practice and is not recommended for production. It can be useful for demonstration purposes, allowing the demonstrator to avoid dealing with two separate files. For better code organization and maintainability, it's recommended to write JavaScript code in a separate .js file and link it using the src attribute in the <script> tag. How does inline JavaScript work with HTML ? Comment More infoAdvertise with us Next Article How to include inline JavaScript in HAML ? S shubhamr238 Follow Improve Article Tags : Technical Scripter JavaScript Web Technologies Similar Reads How to Add JavaScript in HTML Document? To add JavaScript in HTML document, several methods can be used. These methods include embedding JavaScript directly within the HTML file or linking an external JavaScript file.Inline JavaScriptYou can write JavaScript code directly inside the HTML element using the onclick, onmouseover, or other ev 3 min read How to Handle JavaScript Events in HTML ? An Event is an action or occurrence recognized by the software. It can be triggered by the user or the system. Mostly Events are used on buttons, hyperlinks, hovers, page loading, etc. All this stuff gets into action(processed) with the help of event handlers. In this article, you will learn about d 3 min read How to call JavaScript function in HTML ? In HTML, you can easily call JavaScript functions using event attributes like onclick and onload. Just reference the function name within these attributes to trigger it. You can also call functions directly within script blocks using standard JavaScript syntax. Let's create an HTML structure with so 2 min read How to include inline JavaScript in HAML ? There are mainly two ways to include JavaScript in HTML: Internal/Inline: By using "script" element in the "head" or "body" section. External: By using an external JavaScript file. We will be extending the below example for HTML and see how it could be implemented similarly for HAML. To include inli 2 min read How to trigger events in JavaScript ? JavaScript is a high-level, interpreted, dynamically typed client-side scripting language. While HTML is static and defines the structure of a web page, JavaScript adds interactivity and functionality to HTML elements. This interaction is facilitated through events, which are actions or occurrences 2 min read How to get/change the HTML with DOM element in JavaScript ? In order to get/access the HTML for a DOM element in JavaScript, the first step is to identify the element base on its id, name, or its tag name. Then, we can use inner.HTML or outer.HTML to get the HTML. Using the getElementById() method: This method gets/identifies the DOM elements using its ID an 3 min read Like