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 413 of 534
Built-in javascript constructors?
In this article, we are going to discuss about the Built-in JavaScript constructors with appropriate examples in JavaScript. JavaScript has provided some built-in constructors for native objects. These built-in functions include Object(), String(), Boolean(), RegExp(), Number(), Array(), Function(), and Date(). Note: We cannot include the Math object in those built-in constructors because Math is a global object. The new keyword cannot be used with Math. Let's understand this concept in a better way with the help of examples further in this article. String() Constructor The String() constructor converts values to strings. Here's an example demonstrating ...
Read MoreNumber of elements a particular tag contains in JavaScript?
In this article we are going to learn about counting the number of child elements a particular tag contains in JavaScript with suitable examples. There are two main approaches to count child elements in JavaScript. You can use the childElementCount property to count direct child elements, or use the length property with methods like getElementsByClassName() to count elements by class name. Let's explore both methods with practical examples to understand how to count elements effectively. Using the childElementCount Property The childElementCount property returns the number of direct child elements within a specific parent element. It only ...
Read MoreReplace a child node with a new node in JavaScript?
In this article we are going to learn how to replace a child node with a new node in JavaScript with suitable examples. To replace a child node with a new node, JavaScript provides two built-in DOM methods: replaceChild() and replaceWith(). Both methods allow you to swap an existing node with a new one in the DOM tree. The methods replaceChild() and replaceWith() are supported by all modern browsers and are features of the DOM specification. replaceChild() Method The replaceChild() method is called on the parent element to replace one of its child nodes. Syntax ...
Read MoreTag names of body element's children in JavaScript?
In JavaScript, you can access the tag names of child elements within the body using the children property or the querySelector method. The children property returns an HTMLCollection of all child elements, while querySelector allows you to target specific elements using CSS selectors. Using the children property to get all child elements Using the querySelector method to select specific child elements Both approaches provide different ways to access and manipulate child elements within the DOM structure. Using the children Property The DOM children property returns an HTMLCollection of all child ...
Read MoreAn element inside another element in JavaScript?
In JavaScript, accessing elements nested inside other elements is a common task when working with the DOM. We can access inner elements using various methods and check containment relationships using the contains() method. JavaScript provides several ways to access nested elements. The most common approach uses getElementsByTagName() on a parent element, while contains() helps verify parent-child relationships. Accessing Nested Elements To access an element inside another element, we first get the parent element, then search within it: document.getElementById('parentID').getElementsByTagName('tagName')[index] document.getElementById('parentID').getElementsByClassName('className')[index] Method 1: Using getElementsByTagName() This example shows how to access and modify elements ...
Read MoreValue of the class attribute node of an element in JavaScript?
JavaScript provides the getAttributeNode() method to retrieve the attribute node of an element as an attribute object. This method returns the attribute node with the specified name, or null if the attribute doesn't exist. Syntax element.getAttributeNode(attributename); It returns the attribute object representing the specified attribute node. To get the actual value, use the value property of the returned object. Example: Getting Class Attribute Value In this example, we have two heading tags with different classes. We'll use getAttributeNode() to retrieve the class attribute and display its value. ...
Read MoreInsert a specified HTML text into a specified position in the JavaScript document?
The insertAdjacentHTML() method allows you to insert HTML content at specific positions relative to an existing element in the DOM. This method provides four position options and is more efficient than using innerHTML for adding content. Syntax element.insertAdjacentHTML(position, text); Parameters element − The target HTML element where content will be inserted position − One of four values: "beforebegin", "afterbegin", "beforeend", "afterend" text − The HTML string to insert Position Options Explained Target Element ...
Read MoreInsert a specified element in a specified position in JavaScript?
In JavaScript, the insertAdjacentElement() method allows you to insert an existing DOM element at a specific position relative to another element. This method is useful for dynamically rearranging elements without removing them from the DOM first. Syntax element.insertAdjacentElement(position, elementToInsert); Parameters element - The target element where the new element will be positioned relative to position - A string specifying where to insert the element. Four possible values: 'beforebegin' - Before the target element 'afterbegin' - Inside the target element, as the first child 'beforeend' - Inside the target element, as the last ...
Read MoreSibling of a list element in JavaScript?
In JavaScript, you can find sibling elements using DOM traversal properties. Siblings are elements that share the same parent element and exist at the same level in the HTML structure. Available Properties JavaScript provides several properties to navigate between sibling elements: nextSibling - Returns the next node (including text and comment nodes) nextElementSibling - Returns the next element sibling only previousSibling - Returns the previous node (including text and comment nodes) previousElementSibling - Returns the previous element sibling only Syntax node.nextSibling node.nextElementSibling node.previousSibling node.previousElementSibling Key Difference: nextSibling vs nextElementSibling ...
Read MoreFirst and last child node of a specific node in JavaScript?
In JavaScript, you can access the first and last child nodes of any DOM element using built-in properties. There are four key properties: firstChild, lastChild, firstElementChild, and lastElementChild. Key Differences The main difference between these properties is how they handle whitespace and text nodes: firstChild and lastChild return any node, including text nodes (whitespace) and comments firstElementChild and lastElementChild return only HTML element nodes, ignoring whitespace and comments Syntax // First child properties element.firstChild // Returns any node (including text/whitespace) element.firstElementChild // Returns only element ...
Read More