Articles on Trending Technologies

Technical articles with clear explanations and examples

Select and Deselect Text Inside an Element using JavaScript

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

In JavaScript, you can programmatically select and deselect text inside DOM elements using the Selection API. This is useful for creating interactive features like highlighting content or implementing custom text selection controls. Core Methods The key methods for text selection are: window.getSelection().selectAllChildren(element) - Selects all text inside an element window.getSelection().removeAllRanges() - Clears all current selections Example Select and Deselect Text body { ...

Read More

Filtering of JavaScript object

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 288 Views

Filtering JavaScript objects allows you to extract specific key-value pairs based on certain criteria. This is useful when working with large datasets or when you need to display only relevant information. Problem Statement We need to create a function that takes an object and a search string, then filters the object keys that start with the search string and returns a new filtered object. Example: Basic Object Filtering const obj = { "PHY": "Physics", "MAT": "Mathematics", "BIO": "Biology", "COM": "Computer Science", "SST": "Social Studies", ...

Read More

Assign new value to item in an array if it matches another item without looping in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 163 Views

In JavaScript, you can assign a new value to an array item that matches a condition without using traditional loops. This can be achieved using array methods like filter() combined with map(), or more efficiently with forEach() or find(). Using filter() and map() The filter() method finds matching items, and map() modifies them. Here's how to change "Bob" to "Carol" in an array of objects: const studentDetails = [ {Name: "John"}, {Name: "David"}, {Name: "Bob"}, {Name: "Mike"} ]; var ...

Read More

Swap kth element of array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 680 Views

We are required to write a JavaScript function that accepts an array of Numbers and a number, say k (k must be less than or equal to the length of array). And our function should replace the kth element from the beginning with the kth element from the end of the array. Understanding the Problem When we say "kth element from beginning" and "kth element from end", we need to understand the positioning: kth element from beginning: index = k - 1 kth element from end: index = array.length - k Example ...

Read More

How to remove blank (undefined) elements from JavaScript array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 800 Views

When working with JavaScript arrays, you may encounter sparse arrays containing empty slots (undefined elements). These gaps can occur when elements are deleted or when arrays are created with missing values. const arr = [4, 6, , 45, 3, 345, , 56, 6]; console.log(arr); console.log("Length:", arr.length); [ 4, 6, , 45, 3, 345, , 56, 6 ] Length: 9 We need to remove only the undefined and empty values, not all falsy values like 0, false, or empty strings. Method 1: Using splice() with for loop Use a for loop to ...

Read More

How to generate Prime Numbers in JavaScript?

Ayyan
Ayyan
Updated on 15-Mar-2026 3K+ Views

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In JavaScript, we can generate prime numbers using different approaches. Basic Prime Check Algorithm The most straightforward method checks if a number is divisible by any number from 2 to itself minus 1: JavaScript Prime Numbers for (var limit = 1; limit 1) { document.write(limit + ""); } } 2 3 5 7 11 13 17 19 Optimized Prime Generator Function For better performance, we only need to check divisors up to the square root of the number: function isPrime(num) { if (num

Read More

How to apply a function simultaneously against two values of the array from left-to-right?

Manikanth Mani
Manikanth Mani
Updated on 15-Mar-2026 248 Views

The reduce() current) ? prev : current; }); document.write("Maximum value: " + max + ""); // Count occurrences var fruits = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']; var count = fruits.reduce(function(acc, fruit) { ...

Read More

How to set the width of an element in JavaScript?

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 12K+ Views

In this tutorial, we will learn to set the width of an element in JavaScript. We can use the "width" property in JavaScript to set the width of an element. The web page should be responsive on every screen. Each element on a web page should have its area covered. We can set the size of elements by setting their width and height. Let us look at how to set the width of an element in JavaScript. Following is the property by which we can set the width of an element in JavaScript − ...

Read More

Client-side image processing with HTML

Samual Sam
Samual Sam
Updated on 15-Mar-2026 494 Views

Client-side image processing allows you to manipulate images directly in the browser using HTML, CSS, and JavaScript without server uploads. This approach provides instant feedback and reduces server load. HTML Structure for Image Processing Start with a basic HTML structure that includes file input and canvas elements: Client-side Image Processing Image Processing Demo ...

Read More

Filter gray image, black around png in Internet Explorer 8

Vrundesha Joshi
Vrundesha Joshi
Updated on 15-Mar-2026 176 Views

In Internet Explorer 8, applying grayscale filters to PNG images often creates unwanted black backgrounds. This happens because IE8's filter system doesn't handle PNG transparency properly when multiple filters are applied. The Problem When using BasicImage(grayscale=1) filter on PNG images with transparency in IE8, the transparent areas become black instead of remaining transparent. Solution: Combining Gradient and BasicImage Filters The solution is to combine a transparent gradient filter with the grayscale filter in a single filter declaration: progid:DXImageTransform.Microsoft.gradient(startColorstr=#00FFFFFF, endColorstr=#00FFFFFF) progid:DXImageTransform.Microsoft.BasicImage(grayscale=1) Complete CSS Implementation .demo { background: ...

Read More
Showing 16861–16870 of 61,297 articles
Advertisements