Alert for unsaved changes in form in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:18:59

814 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

Strange syntax, what does `?.` mean in JavaScript?

AmitDiwan
Updated on 15-Mar-2026 23:18:59

2K+ Views

The ?. operator is called optional chaining and provides a safe way to access nested object properties without throwing errors when intermediate properties are undefined or null. The Problem with Regular Property Access Consider this nested object describing a person: const being = { human: { male: { age: 23 } } }; console.log(being.human.male.age); 23 This works fine, but what happens if we try to access a property that doesn't exist? ... Read More

Replace HTML div into text element with JavaScript?

AmitDiwan
Updated on 15-Mar-2026 23:18:59

907 Views

To replace HTML div content with text from another element, you can use document.querySelectorAll() to select multiple elements and getElementsByClassName() to get the source text. This approach allows you to dynamically update multiple elements with content from a single source. Example Replace Div Content My Name is John ... Read More

Change string based on a condition - JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:18:59

825 Views

We are required to write a JavaScript function that takes in a string. The task of our function is to change the string according to the following condition − If the first letter in the string is a capital letter then we should change the full string to capital letters. Otherwise, we should change the full string to small letters. Example Following is the code − const str1 = "This is a normal string"; const str2 = "thisIsACamelCasedString"; const changeStringCase = str => { ... Read More

How to create arrays in JavaScript?

Anjana
Updated on 15-Mar-2026 23:18:59

591 Views

JavaScript provides multiple ways to create arrays. Arrays are used to store multiple values in a single variable and can hold different data types. Using Array Literal (Recommended) The most common and recommended way to create arrays is using square brackets: Array Literal Example var animals = ["Dog", "Cat", "Tiger"]; var numbers ... Read More

How to set the bottom position of a positioned element with JavaScript?

Shubham Vora
Updated on 15-Mar-2026 23:18:59

2K+ Views

In this tutorial, we will learn how to set the bottom position of a positioned element with JavaScript. The position property sets how the element should be positioned in the DOM, and the top, bottom, left, and right properties set the final location of the positioned elements. In JavaScript, we have different ways to set the bottom position of a positioned element, and in this tutorial, we will learn two of them − Using the style.bottom Property Using the style.setProperty Method Using the style.bottom Property In ... Read More

Make HTML5 input type="number" accepting dashes

Smita Kapse
Updated on 15-Mar-2026 23:18:59

3K+ Views

To allow HTML5 input fields to accept dashes along with numbers, you cannot use type="number" directly since it only accepts numeric characters. Instead, use type="text" with a pattern attribute to validate the input format. The Problem with type="number" HTML5 input type="number" strictly accepts only numeric values and will reject any non-numeric characters including dashes. To accept dashes, we need to use type="text" with pattern validation. Solution Using Pattern Attribute Use the pattern attribute with a regular expression to validate numbers with dashes: Number Input with Dashes ... Read More

CSS padding-right property

Arjun Thakur
Updated on 15-Mar-2026 23:18:59

266 Views

The padding-right property in CSS specifies the amount of space between the content and the right border of an element. It adds internal spacing on the right side without affecting the element's border or margin. Syntax padding-right: value; The value can be specified in: Length units: px, em, rem, pt, etc. Percentage: % (relative to parent element's width) Keywords: inherit, initial, unset Example: Length Values .box { ... Read More

In how many ways can we find a substring inside a string in javascript?

vineeth.mariserla
Updated on 15-Mar-2026 23:18:59

212 Views

JavaScript provides several methods to find a substring inside a string. The most commonly used approaches are indexOf(), includes(), search(), and regular expressions with match(). Using indexOf() Method Syntax string.indexOf(substring, startPosition) The indexOf() method returns the index of the first occurrence of the substring. If the substring is not found, it returns -1. This method is case-sensitive. Example var company = "Tutorialspoint"; document.write("Index of 'point': " + company.indexOf('point')); document.write(""); document.write("Is 'Tutor' present: " + (company.indexOf('Tutor') !== ... Read More

How to test if a letter in a string is uppercase or lowercase using javascript?

Ayush Gupta
Updated on 15-Mar-2026 23:18:59

8K+ Views

To test if a letter in a string is uppercase or lowercase using JavaScript, you can convert the character to its respective case and compare the result. This method works by checking if the character remains the same after case conversion. Basic Approach The most straightforward method is to compare a character with its uppercase and lowercase versions: function checkCase(ch) { if (!isNaN(ch * 1)) { return 'ch is numeric'; } else { ... Read More

Advertisements