Open In App

How does inline JavaScript work with HTML ?

Last Updated : 11 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:

12121212121321

Output

For 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.



Similar Reads

What is the difference between display: inline and display: inline-block in CSS?
The display property in CSS is a very useful and commonly used property that contains many values. The display property defines how an HTML element should be displayed on the webpage. The property also specifies the type of box used for an HTML element and how it should be laid out on the page. If we need to display the elements that are laid out a
4 min read
How display: inline is different from display: inline-block in CSS ?
In this article, we will know about the display property in CSS, along with understanding the 2 different property values for display property, i.e., display: inline &amp; display: inline-block properties, &amp; will understand their basic differences &amp; implementation through the examples. The display property facilitates setting the element by
3 min read
What is the difference between inline-flex and inline-block in CSS?
The display property specifies how an element should be displayed on a webpage. There can be many values, related to this property in CSS. Inline-block and inline-flex are two such properties. Although there are several values that this property can have, to understand the aforementioned, let us first look into three other values: inline, block, an
3 min read
Inline HTML Helper - HTML Helpers in ASP.NET MVC
HTML Helpers are methods that returns HTML strings. These are used in the view. In simple terms, these are C# methods that are used to return HTML. Using HTML helpers you can render a text box, an area, image tag, etc. In MVC we have many built-in HTML helpers and we can create custom helpers too. Using HTML helpers a view can show model properties
2 min read
How does internationalization work in JavaScript?
Internationalization (also referred to as i18n) is that the method of making or reworking product and services so they'll simply be tailored to specific native languages and cultures. Localization (also referred to as L10n) is that the method of adapting internationalized computer code for a particular region or language. In alternative words, inte
3 min read
How does asynchronous code work in JavaScript ?
In this article, we will learn about asynchronous code working in JavaScript. Let us understand the basic details which are associated with Synchronous JavaScript. Synchronous JavaScript: Synchronous JavaScript implies that the user is having the data which is synchronously accessed as well as available in order.In this, the user tries to access th
3 min read
How does JavaScript .prototype work ?
In JavaScript when adding behaviour to objects, then if creating multiple objects using the constructor (calling the constructor function with the 'new' keyword) then the new keyword is the one that is converting the function call into constructor call and every time a brand new empty object is created which create inconsistency because for each ob
4 min read
How does memory stacks work in Javascript ?
Introduction: The memory stack is a mechanism in JavaScript that allows us to allocate and free memory. If a programmer wants to store some data, they must first create an empty part of the heap that is called a "stack." Then, they can push and then pop values on the stack. When working with strings in JavaScript, we typically use string concatenat
3 min read
How does Array.prototype.slice.call work in JavaScript ?
Array.prototype.slice.call()Array.prototype.slice.call() is a method in JavaScript, It is used to convert an array-like or iterable object into a genuine array. It allows you to extract a portion of an array or an array-like object and return a new array. This technique is often used to manipulate and work with non-array objects to convert them int
2 min read
How does this keyword work in JavaScript ?
In JavaScript, the this keyword refers to the object that is executing the current function. The this keyword refers to different objects depending on how it is used: Global Context:When you're not inside any function or object, this refers to the big global environment.Example in a browser: Imagine the global environment as the entire window. So,
1 min read
How does Query.prototype.gte() work in Mongoose ?
The Query.prototype.gte() function is used to specify a $gte query condition. It returns documents that are greater than or equal the specified condition. Syntax: Query.prototype.gte() Parameters: This function has one parameter val and an optional parameter path.Return Value: This function returns Query Object. Mongoose Installation: npm install m
2 min read
How does Query.prototype.hint() work in Mongoose ?
The Query.prototype.hint() function is used to set the query hints. A hint object is passed as parameter to this function. Syntax: Query.prototype.hint() Parameters: This function has one parameter val which is an hint object.Return Value: This function returns Query Object. Mongoose Installation: npm install mongoose After installing the mongoose
2 min read
How does Query.prototype.mod() work in Mongoose?
The Query.prototype.mod() function is used to specify a $mod condition. This function basically filters documents for documents whose path property is a number that is equal to remainder modulo divisor. Syntax: Query.prototype.mod() Parameters: This function has one optional path parameter and an array parameter which must be of length 2 where the
2 min read
How does Query.prototype.getUpdate() work in Mongoose?
The Query.prototype.getUpdate() function is used to return the current update operations as a JSON object. It is easy to use and user can easily get the current update operations. Syntax: Query.prototype.getUpdate() Parameters: This function has no parameter.Return Value: This function returns Query Object.Installing mongoose : npm install mongoose
2 min read
How does maxScan() work in Mongoose ?
The maxScan() function is used to specify the maxScan option. This option tell the MongoDB server to set the maximum limit of scanning documents when it performs scan operation. Syntax: maxScan() Parameters: This function has one val parameter of number type.Return Value: This function returns Query Object. Installing mongoose module: npm install m
2 min read
How does Query.prototype.centerSphere() work in Mongoose ?
The Query.prototype.centerSphere() function defines a circle for a geospatial query that uses spherical geometry. It returns the all the documents which are within the bounds of the circle. Syntax: Query.prototype.centerSphere() Parameters: This function has one value parameter, and an optional path parameter.Return Value: This function returns Que
2 min read
How does Query.prototype.explain() work in Mongoose?
The Query.prototype.explain() function is used to set the explain option thereby making this query return detailed execution stats instead of the actual query result. Syntax: Query.prototype.explain() Parameters: This function has one optional verbose parameter.Return Value: This function returns Query Object. Installing mongoose : npm install mong
2 min read
How does Query.prototype.exec() work in Mongoose?
The Query.prototype.exec() function is used to execute the query. It can handle promises and executes the query easily. The callback can be passed as an optional parameter to handle errors and results. Syntax: Query.prototype.exec() Parameters: This function has two optional parameters, one is callback function and another is operation of string or
2 min read
How does Query.prototype.error() work in Mongoose?
The Query.prototype.error() function is used to sets/gets the error flag on this query. If this flag is undefined or not null then the exec() promise will reject without executing. Syntax: Query.prototype.error() Parameters: This function has one err parameter of Error type.Return Value: This function returns Query Object.Installing mongoose : npm
2 min read
How does Query.prototype.equals() work in Mongoose ?
The Query.prototype.equals() function is used to Specify the complementary comparison value for paths specified with where() function.Syntax: Query.prototype.equals() Parameters: This function has one val parameter of object type.Return Value: This function returns Query Object.Installing mongoose : npm install mongoose After installing the mongoos
2 min read
How does Query.prototype.elemMatch() work in Mongoose ?
The Query.prototype.elemMatch() function is used to specify the $elemMatch condition. It matches the documents that contain an array field with at least single or one element that will matches all the specified query criteria. Syntax: Query.prototype.elemMatch() Parameters: This function has two parameters, ie. path and filter. Return Value: This f
2 min read
How does Query.prototype.distinct() work in Mongoose?
The Query.prototype.distinct() function declares or executes a distinct() operation. And if we pass a callback, then the query executes.Syntax: Query.prototype.distinct() Parameters: This function has three optional parameters ie. field, filter and callback function.Return Value: This function returns Query Object.Installing mongoose : npm install
2 min read
How does Query.prototype.cursor() work in Mongoose?
The Query.prototype.cursor() function returns a wrapper around a MongoDB driver cursor. And this function triggers the pre find hooks, not the post find hooks.Syntax: Query.prototype.cursor() Parameters: This function has one optional parameter i.e. transform which is an optional function which accepts a mongoose document.Return Value: This functio
2 min read
How does Query.prototype.comment() work in Mongoose ?
The Query.prototype.comment() is used to specify the comment options. It makes possible to attach a comment to a query in any context that $query may appear.Syntax: Query.prototype.comment() Parameters: This function has one array parameter i.e. comment of string type.Return Value: This function returns Query Object. Installing mongoose: npm instal
2 min read
How does Query.prototype.catch() work in Mongoose ?
The Query.prototype.catch() function executes the query and returns a promise which can be resolved with either the doc(s) or rejected with the error.Syntax: Query.prototype.catch() Parameters: This function has one parameter i.e. a reject callback function.Return Value: This function returns a promise. Installing mongoose : npm install mongoose Af
2 min read
How does Query.prototype.collation() work in Mongoose?
The Query.prototype.collation() allows users to specify language-specific rules for various comparison like string comparison such as rules for accent marks or rules for letter case, etc.Syntax: Query.prototype.collation() Parameters: This function has one array parameter i.e. an object which can have various properties like locale, caseLevel, case
2 min read
How does Query.prototype.cast() work in Mongoose ?
The Query.prototype.cast() function casts this query to the schema of the model. And if the obj is present, then it is casted instead of this query.Syntax: Query.prototype.cast() Parameters: This function has one parameter i.e. the model to cast and if not set, then the default is this.model.Return Value: This function returns Query Object.Installi
2 min read
How does Query.prototype.box() work in Mongoose ?
The Query.prototype.box() is used to specify a $box condition. When this function is used, the $geoWithin returns documents based on the grid coordinates.Syntax: Query.prototype.box() Parameters: This function has one array object parameter which has upper-left co0rdinates and upper-right coordinates.Return Value: This function returns Query Object
2 min read
How does Query.prototype.batchSize() work in Mongoose ?
The Query.prototype.batchSize() is used to set the batchSize option. The batchSize() function basically instructs the driver to retrieve a certain number of items per time.Syntax: Query.prototype.batchSize() Parameters: This function has one array parameter i.e. number defining the batch size.Return Value: This function returns Query Object.Install
2 min read
How does Query.prototype.and() work in Mongoose ?
The Query.prototype.and() specifies arguments for a $and condition. The array parameter contains all the conditions that has to be matched.Syntax: Query.prototype.and() Parameters: This function has one array parameter i.e. an array of conditions.Return Value: This function returns Query Object.Installing mongoose : npm install mongoose After insta
2 min read