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
Web Development Articles
Page 482 of 801
How to find out what character key is pressed in JavaScript?
To find out which character key is pressed in JavaScript, you can use various methods. The modern approach uses the key property, while legacy methods relied on keyCode. Modern Approach: Using event.key The key property directly returns the character that was pressed, making it the preferred method: Key Detection - Modern Method Type in the input field below: ...
Read MoreSorting Array with JavaScript reduce function - JavaScript
We are required to write a JavaScript function that takes in an array of numbers. The function should sort the array without using the Array.prototype.sort() method. We are required to use the Array.prototype.reduce() method to sort the array. Let's say the following is our array: const arr = [4, 56, 5, 3, 34, 37, 89, 57, 98]; Understanding the Approach The reduce() method can be used to build a sorted array by iterating through each element and placing it in the correct position within an accumulator array. We'll use insertion sort logic within the ...
Read MoreSimplest way to detect keypresses in JavaScript?
The simplest way to detect keypresses in JavaScript is using the onkeypress event handler. However, modern JavaScript also provides addEventListener for better event handling. document.onkeypress The key press is matched with the keyCode property, which returns the Unicode character code of the key that triggered the onkeypress event. Using document.onkeypress Keypress Detection Press any key to see the detection in action! ...
Read MoreTrying to get number for each character in string - JavaScript
We are required to write a JavaScript function that takes in a string. It should print out each number for every corresponding letter in the string. For example, a = 1 b = 2 c = 3 d = 4 e = 5 . . . Y = 25 Z = 26 Therefore, if the input is "hello man", Then the output should be a number for each character − "8, 5, 12, 12, 15, 13, 1, 14" How It Works The solution uses the charCodeAt() method to get ...
Read MoreConvert HTML table to array in JavaScript?
Converting an HTML table to an array in JavaScript involves extracting data from table cells and storing them in a structured format. This is commonly done using DOM manipulation methods or jQuery. HTML Table Structure Let's start with a sample HTML table: Name Age John 23 ...
Read MoreCheck if value is empty in JavaScript
In JavaScript, checking if a value is empty is essential for form validation and data processing. You can check for empty strings, null values, and undefined variables using various approaches. Basic Empty Check The most common approach is to check for empty strings and null values: Check Empty Value USERNAME: ...
Read MoreHow to capitalize the first letter of each word in a string using JavaScript?
In JavaScript, you can capitalize the first letter of each word in a string by splitting the string into individual words, capitalizing each word's first letter, and then joining them back together. Syntax string.split(' ') // Split string into array of words word.charAt(0) // Get first character word.toUpperCase() // Convert to uppercase word.substring(1) // Get ...
Read MoreHow to replace leading zero with spaces - JavaScript
We are required to write a JavaScript function that takes in a string that represents a number. Replace the leading zero with spaces in the number. We need to make sure the prior spaces in number are retained. For example, If the string value is defined as − " 004590808" Then the output should come as − " 4590808" Example Following is the code − const str = ' 004590808'; const replaceWithSpace = str => { let replaced = ''; const regex = new RegExp(/^\s*0+/); ...
Read MoreHow to inherit CSS properties of parent Element using JavaScript?
JavaScript provides several ways to make child elements inherit CSS properties from their parent elements. You can use classList.add() to apply CSS classes, or directly manipulate the style property to inherit specific styles. Using classList.add() Method The most common approach is creating elements and adding CSS classes that define inheritance rules. CSS Inheritance Example .parent-container { ...
Read MoreLose input hints (get focus) after pressing mouse cursor in a textbox with JavaScript
In this tutorial, we'll learn how to clear placeholder-like text from an input field when the user clicks on it (gets focus) and restore it when the field loses focus if no text was entered. Let's say we have the following input field with hint text: StudentName: To lose input hints on pressing mouse cursor, we use the onfocus and onblur events. How It Works The solution uses two JavaScript functions: onfocus: Clears the hint text when user clicks in the field onblur: Restores hint ...
Read More