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 236 of 534
How to set cursor position in content-editable element using JavaScript?
In HTML, content editable div allows users to edit the content of the div element. We need to pass the contenteditable attribute with true Boolean value to the div element to make any div element editable. The content editable div contains the caret cursor by default, and sometimes we may require to set the caret cursor position in the content editable div element to edit the content of the div. However, we can set the caret cursor position anywhere by clicking at a particular place in the content editable div. This tutorial will teach us to use different ...
Read MoreHow to set cursor style to pointer for links without href?
We can use the tag in HTML to add a link to the web page. The default cursor style for the elements is the pointer, but removing the href attribute from the tag changes the cursor style. So, in this tutorial, we will learn to keep the cursor style to pointer for tags without the href attribute. Users can follow the example below to check out the default cursor style for the elements. Default Behavior Example In the example below, we have used the tag to create three different links. ...
Read MoreHow to set default values when destructuring an object in JavaScript?
The array and object destructuring feature was introduced in the ES6 version of JavaScript. The destructuring of the array and object allows us to store its values in a separate variable. After that, we can use that variable to access the value of the particular property of the object. The main thing we need to focus on while destructuring the array object is the default values. For example, we have added the property3 variable in the destructuring object, but if the object doesn't contain the property3, destructuring sets the undefined value to the property3 variable. Users can follow ...
Read MoreHow to Sort/Order keys in JavaScript objects ?
In JavaScript, objects store key-value pairs, but their keys aren't automatically sorted. While we can't directly use the sort() method on objects like we do with arrays, we can sort the keys and create a new object with the sorted key-value pairs. Below, we will learn various methods to sort object keys in ascending and descending order. Using sort() Method to Order Keys We can use Object.keys() to get all object keys as an array, sort that array, then reconstruct the object with sorted keys. Syntax let allKeys = Object.keys(originalObject); allKeys.sort(); let sortedObj = ...
Read MoreHow to solve JavaScript heap out of memory on prime number?
The 'heap out of memory' error occurs when JavaScript code exceeds the allocated memory limit. This commonly happens with inefficient algorithms that have high time or space complexity, especially when processing large numbers. When finding prime factors of very large numbers, naive algorithms can quickly exhaust available memory. The key is optimizing the algorithm to reduce both time and space complexity. The Problem: Inefficient Prime Factor Algorithm The following example demonstrates how a poorly optimized algorithm causes memory issues when processing large numbers: Visualizing Memory Error with Large Numbers ...
Read MoreHow can a page be forced to load another page in JavaScript?
In JavaScript, you can force a page to load another page using the window.location object. This object provides three main approaches: window.location.href property, window.location.assign() method, and window.location.replace() method. Each handles browser history differently, making them suitable for different use cases. Using window.location.href The window.location.href property contains the current URL and can redirect users to a new page. This is the most commonly used method for page redirection. Syntax window.location.href = "new_url"; You can also use setTimeout() to redirect after a delay: Delayed Redirect Example ...
Read MoreHow to create an array with random values with the help of JavaScript?
In JavaScript, creating arrays filled with random values is a common requirement for simulations, games, and testing. The Math.random() method generates random numbers between 0 (inclusive) and 1 (exclusive), which can be combined with various array methods to create arrays of random values. Using Math.random() with For Loop The most straightforward approach uses a for loop with Math.random() to populate an array: Syntax // Random decimals between 0 and 1 for (let i = 0; i < arrayLength; i++) { randomArr.push(Math.random()); } // Random integers in a range for (let ...
Read MoreHow to set full-screen iframe with height 100% in JavaScript?
Setting an iframe to full-screen with 100% height is a common requirement in web development. This can be achieved using JavaScript's style.height property along with additional CSS properties to create a proper full-screen experience. The style.height property sets or returns the height of an HTML element as a string value. When combined with other style properties like position: fixed and proper positioning, it creates a true full-screen iframe overlay. Syntax element.style.height = "100%"; element.style.width = "100%"; element.style.position = "fixed"; element.style.top = "0"; element.style.left = "0"; element.style.zIndex = "9999"; Parameters ...
Read MoreHow to set location and location.href using JavaScript?
The window.location object in JavaScript provides methods to get and set the current page URL. You can redirect users to different pages using either location or location.href properties. The window.location object can be written without the window prefix. Common location properties include: location.href − Gets or sets the complete URL location.hostname − Gets the domain name location.protocol − Gets the protocol (http: or https:) location.pathname − Gets the path portion of the URL location.assign() − Loads a ...
Read MoreHow to set stroke width of a canvas circle using Fabric.js ?
The stroke and strokeWidth properties are used to set the stroke color and width of a canvas circle in Fabric.js. These properties allow you to customize the border appearance of circle objects. The Fabric.js Circle class provides a rich set of features for creating interactive circles. Unlike basic HTML5 canvas circles, Fabric.js circles are movable, resizable, and highly customizable with properties for stroke, fill, dimensions, and positioning. Syntax fabric.Circle({ radius: number, stroke: string, strokeWidth: number }); Parameters radius − Specifies the radius of the ...
Read More