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
Articles by AmitDiwan
Page 456 of 840
Remove any text not inside element tag on a web page with JavaScript?
To remove text nodes that are not inside HTML element tags, we use jQuery's contents() and filter() methods to identify text nodes, then remove() to delete them. Understanding Text Nodes vs Element Nodes In the DOM, there are different node types: Element nodes (nodeType = 1): HTML tags like , Text nodes (nodeType = 3): Plain text content not wrapped in tags Example HTML Structure Consider this HTML where we want to remove the unwrapped text: Demo Program This is also Demo Program The text "This is also Demo ...
Read MoreHow do I trigger a function when the time reaches a specific time in JavaScript?
To trigger a function at a specific time in JavaScript, calculate the time difference between the current time and target time, then use setTimeout() with that delay. Basic Approach Extract the target time, calculate the milliseconds until that time, and schedule the function execution: Trigger Function at Specific Time Function will trigger at specified time Waiting for target time... ...
Read MoreSplitting string into groups – JavaScript
Given a string S, consisting of alphabets, numbers and special characters. We need to write a program to split the strings in three different strings S1, S2 and S3, such that − The string S1 will contain all the alphabets present in S, The string S2 will contain all the numbers present in S, and S3 will contain all special characters present in S. The strings S1, S2 and S3 should have characters in the same order as they appear in input. Syntax const ...
Read MoreHow to create a speed converter with HTML and JavaScript?
To create a speed converter with HTML and JavaScript, we'll build a simple web application that converts kilometers per hour to meters per second. This involves HTML for the interface and JavaScript for the conversion logic. HTML Structure The HTML creates the user interface with an input field for entering speed values and a display area for the converted result: body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; padding: 20px; ...
Read MoreBetter ways to modify string with multiple methods using JavaScript
JavaScript provides several string methods that can be combined to modify and format strings effectively. You can use methods like toLowerCase(), toUpperCase(), charAt(), and slice() together to achieve proper string formatting. Let's say we have the following mixed-case string: var sentence = "tHIS iS tHE JavaScript pROGRAM"; console.log("Original:", sentence); Original: tHIS iS tHE JavaScript pROGRAM Method 1: Using charAt() and slice() This approach capitalizes the first letter and converts the rest to lowercase: var sentence = "tHIS iS tHE JavaScript pROGRAM"; function modifyStringWithMultipleMethods(sentence) { ...
Read MoreSwap certain element from end and start of array - JavaScript
We need to write a JavaScript function that accepts an array of numbers and a position k, then swaps the kth element from the beginning with the kth element from the end of the array. Understanding the Problem For an array with indices 0 to n-1, the kth element from start is at index k-1, and the kth element from end is at index n-k. We swap these two elements. Example Implementation const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; const swapNth = (arr, k) => { ...
Read MoreProgram to check/uncheck checkboxes using JavaScript
We will learn how to check or uncheck checkboxes in JavaScript in this article, and how we can use it to allow users to make multiple selections on web pages. Checkboxes are one of the most used HTML elements in forms and user interfaces that allow users to pick multiple options. In JavaScript, we can dynamically check or uncheck checkboxes based on certain conditions or user interactions. Let's look at some examples to understand the concept better: Basic Check All/Uncheck All In this example, we will: Create 3 different checkboxes with ...
Read MoreFind distinct elements - JavaScript
We are required to write a JavaScript function that takes in an array of literals, such that some array elements are repeated. We are required to return an array that contains elements that appear only once (not repeated). For example: If the array is: const arr = [9, 5, 6, 8, 7, 7, 1, 1, 1, 1, 1, 9, 8]; Then the output should be: const output = [5, 6]; Using indexOf() and lastIndexOf() The first approach uses indexOf() and lastIndexOf() to check if an element appears only once. If ...
Read MoreChecking whether the sum of digits of a number forms a Palindrome Number or not in JavaScript
We are required to write a JavaScript function that takes in a number, sums its digits and checks whether that sum is a Palindrome number or not. The function should return true if the sum is Palindrome, false otherwise. For example, if the number is 697, then the sum of its digits will be 6 + 9 + 7 = 22, which indeed, is a Palindrome number. Therefore, our function should return true for 697. Understanding the Problem A palindrome number reads the same forwards and backwards. For example: 11, 22, 121, 1331 are palindromes. Our task ...
Read MoreHow to add delay in a loop in JavaScript?
Loops are used in JavaScript to repeat actions or iterate over data. Adding delays between iterations helps control execution speed, create animations, or prevent overwhelming the browser. This article demonstrates how to add delays in JavaScript loops using different techniques. Let's explore practical examples to understand the concept better. Using setTimeout() with Recursive Functions The setTimeout() approach uses recursion to process each array element with a delay: function delayedLoop() { var items = [1, 2, 3, 4, 5]; var delay = 1000; // 1 second ...
Read More