Articles on Trending Technologies

Technical articles with clear explanations and examples

Toggle hide class only on selected div with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 565 Views

To toggle hide class only on selected div, you need to set event on click button. Let's say you need to hide a specific div on the click of + sign. To get font + or - icon, you need to link font-awesome: HTML Structure The HTML contains multiple customer sections, each with a toggle button that can show/hide specific content: Toggle Hide Class Example ...

Read More

Remove all whitespaces from string - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 317 Views

We are required to write a JavaScript function that takes in a string and returns a new string with all the characters of the original string but with whitespaces removed. Method 1: Using a For Loop This approach iterates through each character and builds a new string excluding spaces: const str = "This is an example string from which all whitespaces will be removed"; const removeWhitespaces = str => { let newStr = ''; for(let i = 0; i < str.length; i++){ ...

Read More

How to merge an array with an object where values are arrays - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 221 Views

In JavaScript, you can merge an array with an object containing array values by mapping array elements to object keys sequentially. This creates a new structure where each group contains key-value pairs. Problem Statement Given an array of values and an object with arrays as values, we want to distribute the array values across the object's arrays as keys: const arr = [1, 2, 3, 4, 5]; const obj = { group1: ["Ram", "Mohan", "Shyam"], group2: ["Jai", "Dinesh"], }; // Expected output: const expected = { ...

Read More

How do you check that a number is NaN in JavaScript?

Krantik Chavan
Krantik Chavan
Updated on 15-Mar-2026 370 Views

NaN is a JavaScript property representing "Not-a-Number" value. It indicates that a value is not a legal number. JavaScript provides two methods to check for NaN values: Number.isNaN() and the global isNaN() function. Syntax Number.NaN Number.isNaN(value) isNaN(value) Using Number.isNaN() (Recommended) Number.isNaN() is the most reliable method as it only returns true for actual NaN values without type coercion: Check with Number.isNaN() function display() { ...

Read More

How to set the horizontal alignment of text with JavaScript?

Srinivas Gorla
Srinivas Gorla
Updated on 15-Mar-2026 880 Views

The textAlign property in JavaScript controls the horizontal alignment of text within an element. You can set it to values like left, right, center, or justify. Syntax element.style.textAlign = "alignment_value"; Available Alignment Values left - Aligns text to the left (default) right - Aligns text to the right center - Centers the text justify - Stretches text to fill the line width Example: Setting Right Alignment ...

Read More

The dragLeave event fires before drop for HTML5 drag and drop events

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 407 Views

In HTML5 drag and drop operations, the dragLeave event can sometimes fire unexpectedly before the drop event, causing visual inconsistencies or premature cleanup of drop zone styling. This occurs due to event bubbling and the complex nature of drag operations. Understanding the Problem The dragLeave event fires when the dragged element leaves the boundaries of a drop target. However, it can also fire when moving between child elements within the same drop zone, causing unwanted behavior. Basic Drag and Drop Setup Here's a complete working example that demonstrates the issue and solution: ...

Read More

What is onclick not working for two or more links for same function in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 407 Views

When onclick events don't work for multiple elements with the same function, it's usually because you're only attaching the event to one element or using incorrect selectors. The solution is to use document.querySelectorAll() to select all matching elements and attach event listeners to each one. The Problem A common mistake is trying to attach a click event to multiple elements using document.querySelector(), which only selects the first matching element, or using incorrect event binding methods. Solution: Using querySelectorAll() with Event Listeners Here's how to properly attach click events to multiple elements: ...

Read More

Find all prime factors of a number - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

We are required to write a JavaScript function that takes in a number and returns an array of all the prime numbers that exactly divide the input number. For example, if the input number is 18, the prime factors are 2 and 3 because 18 = 2 × 3². Then the output should be − [2, 3] Understanding Prime Factorization Prime factorization breaks down a number into its prime components. A prime factor is a prime number that divides the original number exactly. Method 1: Using Helper Function to Check Primes ...

Read More

How to set text font family in HTML?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 28K+ Views

To change the text font family in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML tag, with the CSS property font-family. HTML5 does not support the tag, so CSS styles are used to control font appearance. Keep in mind that the usage of the style attribute overrides any style set globally. It will override any style set in the HTML tag or external style sheet. Syntax Content here Example: Basic Font ...

Read More

How can I trigger a JavaScript click event?

Prabhas
Prabhas
Updated on 15-Mar-2026 2K+ Views

In JavaScript, you can programmatically trigger a click event on an element using the click() method. This is useful for automating user interactions or creating custom behaviors. Using the click() Method The click()

Read More
Showing 18971–18980 of 61,297 articles
Advertisements