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
Front End Technology Articles
Page 389 of 652
Tag 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 MoreSum of all prime numbers in an array - JavaScript
We are required to write a JavaScript function that takes in an array of numbers and returns the sum of all the prime numbers present in the array. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example, 2, 3, 5, 7, 11 are prime numbers. Let's say the following is our array: const arr = [43, 6, 6, 5, 54, 81, 71, 56, 8, 877, 4, 4]; The function should sum the prime numbers: 43 + 5 + 71 ...
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 MoreFirst element and last element in a JavaScript array?
An array is a group of elements where each element has its own index value. We can access any element using these indexes. For the first element, the index is always 0, but for the last element, we need to use the array's length property to calculate the correct index. Accessing the First Element Since arrays are zero-indexed in JavaScript, the first element is always at index 0. If the array is arr, then the first element is arr[0]. Example In the following example, we have two arrays and we'll access their first elements using index ...
Read MoreFinding reversed index of elements in arrays - JavaScript
We need to create a JavaScript function that finds the reversed index of an element in an array without actually reversing it. This function calculates what position an element would occupy if the array were reversed. If the element is not present in the array, the function returns -1. Otherwise, it returns the index position the element would have in a reversed array. Understanding Reversed Index The reversed index is calculated using the formula: length - originalIndex - 1 For example, in array [45, 74, 34, 32, 23, 65]: Element 23 is at index 4 ...
Read More