Javascript Articles

Page 438 of 534

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

Program to retrieve the text contents of the user selection using JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 169 Views

In JavaScript, you can retrieve the text contents of a user's selection using the window.getSelection() method. This is useful for creating text highlighting features, copy functionality, or interactive reading applications. How It Works The window.getSelection() method returns a Selection object representing the range of text selected by the user. You can convert this to a string using the toString() method. Example Text Selection Demo body ...

Read More

Circle coordinates to array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 519 Views

In JavaScript, you can generate circle coordinates by using trigonometric functions to calculate points around a circle's circumference. This is useful for animations, graphics, and positioning elements in circular patterns. How Circle Coordinates Work A circle's coordinates are calculated using the parametric equations: X coordinate: centerX + radius × cos(angle) Y coordinate: centerY + radius × sin(angle) By dividing the circle into equal steps and calculating coordinates at each angle, we can create an array of points around the circle. Example ...

Read More

JavaScript code to find the coordinates of every link in a page

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 229 Views

JavaScript provides several methods to find the position and coordinates of elements on a page. For links, we can use properties like offsetLeft, offsetTop, offsetWidth, and offsetHeight to get their position and dimensions. Understanding Element Coordinates In JavaScript, element coordinates are measured from the top-left corner of the page: offsetLeft - Distance from the left edge of the page offsetTop - Distance from the top edge of the page offsetWidth - Width of the element including padding and borders offsetHeight - Height of the element including padding and borders Example ...

Read More

Alert for unsaved changes in form in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 789 Views

Preventing data loss is crucial in web forms. JavaScript's beforeunload event allows you to warn users when they attempt to leave a page with unsaved changes. Basic Implementation The simplest approach uses the beforeunload event to show a browser confirmation dialog: Form Unsaved Changes Alert body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; ...

Read More

How to check whether a Button is clicked with JavaScript?

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

In JavaScript, you can detect button clicks using event listeners. The addEventListener() method is the standard approach to handle click events and track user interactions. Basic Click Detection The most common way to check if a button is clicked is using the click event: Button Click Detection body { font-family: "Segoe UI", Tahoma, Geneva, ...

Read More

What are the uses of the JavaScript WITH statement?

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

The WITH statement is used to specify the default object for the given property and allow us to prevent writing long lengthy object references. It adds the given object to the head of the scope chain. Important: The with statement is deprecated and not recommended in modern JavaScript. It's disabled in strict mode and can cause performance and security issues. Syntax with (object) { // statements } Basic Example Here's how the with statement works with a simple object: ...

Read More

How to get the child element of a parent using JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 632 Views

JavaScript provides several methods to access child elements of a parent element. The most common approaches include using children, childNodes, querySelector, and querySelectorAll. Using the children Property The children property returns a live HTMLCollection of all direct child elements, excluding text nodes and comments. Getting Child Elements body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .child1, .child2, .child3 { display: none; ...

Read More

Can I verify if a JavaScript variable is loaded if so, how?

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

To verify if a JavaScript variable has been loaded or initialized, you can check if it is undefined or has a null value. JavaScript provides several methods to perform this check effectively. The most common approaches include direct comparison with undefined and null, using the typeof operator, or checking if the variable exists in a specific scope. Method 1: Using Direct Comparison This method checks if a variable is undefined or null using strict equality: Variable Check Example ...

Read More

How to pass arguments to anonymous functions in JavaScript?

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

Anonymous functions in JavaScript are functions without a name that can accept parameters just like regular functions. You can pass arguments to them through function calls, event handlers, or higher-order functions. What are Anonymous Functions? Anonymous functions are functions declared without a name. They're often assigned to variables or used as callback functions. Anonymous Functions Anonymous Function Examples Run Examples ...

Read More
Showing 4371–4380 of 5,340 articles
« Prev 1 436 437 438 439 440 534 Next »
Advertisements