Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Javascript Articles
Page 490 of 534
Default exports vs Named exports in JavaScript
In this article, we will learn the difference between default exports and named exports in JavaScript, and how we can use them to effectively organize our code structure. In JavaScript, we can use default exports and named exports to have separate files or modules for separate pieces of code. This helps in enhancing code readability and tree shaking to a great extent. Default exports Named exports A ...
Read MoreHow to add a new element to HTML DOM in JavaScript?
In JavaScript, adding new elements to the HTML DOM involves creating elements and inserting them into the document structure. This process requires understanding three key methods: createElement(), createTextNode(), and appendChild(). Core Methods for DOM Manipulation The Document object provides essential methods for creating and adding elements to the DOM: document.createElement(tagName) - Creates a new HTML element document.createTextNode(text) - Creates a text node appendChild(element) - Adds an element as the last child insertBefore(newElement, referenceElement) - Inserts an element before a reference element Syntax // Create element const element = document.createElement(tagName); // Create ...
Read MoreHow to add an element to a JavaScript object?
In JavaScript, objects are real-time entities that contain properties and methods. Objects store data in key-value pairs, where keys are called properties and values are called property values. There are multiple ways to add new properties to existing JavaScript objects. Let's explore the most common approaches. Object Creation Syntax var obj = new Object(); Or using object literal syntax: var obj = {property1: value1, property2: value2}; Using Dot (.) Operator The dot operator is the most common way to add properties to JavaScript objects. It acts as a connector ...
Read MoreHow to detect a mobile device with JavaScript?
In this tutorial, we are going to learn how can we detect a mobile device over which we are working using JavaScript. Approach 1: Using the navigator.userAgent Property To detect a mobile device with JavaScript we are going to use the window navigator object which contains all the information regarding a browser. The property which we will use to detect a mobile device will be the userAgent property which is used to return the user-agent header sent to the server by the browser. The value we receive from this property is the browser name, version, and platform i.e., ...
Read MoreHow to pad a number with leading zeros in JavaScript?
In this tutorial, we will learn how to pad a number with leading zeros in JavaScript. Padding a number can be done in different ways in JavaScript. In this tutorial, we will see the two most popular ways of doing it − Using the String padStart() method Using the Array object constructor and join() method Using the String padStart() Method In JavaScript, the String object's padStart() method is useful to pad a number with leading zeros. It takes two parameters, the first one is the total length of the targeted or ...
Read MoreHow to stop form submission using JavaScript?
In this tutorial, we will see the methods to stop form submission using JavaScript. Generally, the HTML form by default submits automatically when certain events are triggered. The automatic submission causes the browser to refresh and reload the whole page, which we don't want in some cases. To perform any operation before submission, we need to change the default behavior of the form to prevent it from submitting. Following are the methods we can use to stop form submission: Using the "return false" value Using the preventDefault() method Let ...
Read MoreHow to remove existing HTML elements in JavaScript?
This article discusses how to delete existing HTML elements in JavaScript. To remove HTML elements, you first need to select the element from the document, then use methods like remove() and removeChild() to delete them from the DOM. Using the remove() Method The remove() method directly removes an element from the DOM. It's the simplest approach for modern browsers. Syntax element.remove(); Example Remove Element Example This heading will be removed This paragraph will remain ...
Read MoreHow to check whether a checkbox is checked in JavaScript?
In this tutorial, we will learn to check whether a checkbox is checked in JavaScript. The checkbox is an input type in HTML that works as a selection box. Unlike radio buttons which allow users to select only one value from a group, checkboxes allow users to select multiple values. HTML can add checkboxes to webpages, but JavaScript is needed to add behavior based on whether checkboxes are checked or not. We'll learn to check both single and multiple checkboxes. Check if a Single Checkbox is Selected To check if a checkbox is selected, we access the ...
Read MoreHow to use an HTML button to call a JavaScript function?
We use the onclick event attribute property of the HTML button to call a JavaScript function. The JavaScript code provided in the onclick attribute executes when the button is clicked. There are various attributes provided with the button tag in HTML that allows us to customize the button's functionality and also let us decide how and what the button triggers. Using the onclick Event in JavaScript The onclick event of the button element expects JavaScript code that is triggered when the button is clicked upon. So we put the function that needs to be called in the onclick ...
Read MoreHow to reset or clear a form using JavaScript?
This tutorial teaches us how to reset or clear a form using JavaScript. This option is very helpful for users when they have filled the wrong data and want to clear everything at once. The reset() method provides an efficient way to restore all form fields to their initial values. The reset() method is a built-in JavaScript function that clears all input fields in a form and restores them to their default values. Syntax document.getElementById("formId").reset(); // or document.forms["formName"].reset(); The getElementById() method retrieves the form element by its ID, and then reset() clears all form ...
Read More