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
Object Oriented Programming Articles
Page 170 of 589
Make HTML text input field grow as I type in JavaScript?
Making HTML text input fields grow automatically as users type enhances user experience by eliminating the need to scroll within small input boxes. There are several approaches to achieve this dynamic resizing behavior. Using Contenteditable Span The simplest approach uses a element with contenteditable="true", which automatically expands as content is added: Growing Input Field .growing-input { ...
Read MoreDisplay resultant array based on the object's order determined by the first array in JavaScript?
When working with objects and arrays in JavaScript, you often need to reorder data based on a specific sequence. This article shows how to use an array to determine the order of values extracted from an object using the map() method. The Problem Consider an object containing key-value pairs and an array that defines the desired order. We want to create a new array with values from the object, ordered according to the array sequence. // Object with key-value pairs var lastName = { "John": "Smith", "David": "Miller", ...
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 MoreChecking a radio in radio group with JavaScript?
In JavaScript, you can programmatically check a radio button in a radio group by setting its checked property to true. This is useful for form validation, user interactions, or setting default selections dynamically. HTML Radio Button Group Structure First, let's look at a typical radio button group structure: Gender: Male Female Method 1: Using getElementsByName() The most direct approach is to use getElementsByName() to target radio buttons by their name attribute: ...
Read MorePrettify JSON data in textarea input in JavaScript?
JSON data can become difficult to read when it's minified or poorly formatted. JavaScript provides built-in methods to prettify JSON data in textarea elements using JSON.parse() and JSON.stringify(). How It Works The process involves three steps: Parse the JSON string using JSON.parse() to validate and convert it to an object Use JSON.stringify() with spacing parameters to format the object Replace the textarea content with the prettified JSON Example JSON Prettifier ...
Read MoreSet a default value for the argument to cover undefined errors while calling a function in JavaScript
In JavaScript, when a function is called without arguments or with undefined values, you can set default parameter values to prevent errors and provide fallback behavior. Basic Default Parameter Syntax The simplest way to set default values is using the ES6 default parameter syntax: function functionName(parameter = defaultValue) { // function body } Example: Simple Default Parameters function greet(name = 'Guest') { console.log(`Hello, ${name}!`); } greet(); // No argument passed greet('Alice'); ...
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 create an increment of 10 value once you click a button in JavaScript?
To create a button that increments a value by 10 each time it's clicked, you can use JavaScript's click() event listener along with parseInt() to handle numeric operations. Basic Approach The core concept involves storing a counter variable, adding 10 to it on each button click, and updating the display element with the new value. Example Increment by 10 10 0 ...
Read MoreReturn 5 random numbers in range, first number cannot be zero - JavaScript
We are required to write a JavaScript function that generates an array of exactly five unique random numbers. The condition is that all the numbers have to be in the range [0, 9] and the first number cannot be 0. Example Following is the code − const fiveRandoms = () => { const arr = [] while (arr.length < 5) { const random = Math.floor(Math.random() * 10); if (arr.indexOf(random) > -1){ ...
Read MoreHow to add a new object into a JavaScript array after map and check condition?
In JavaScript, you can add new objects or properties to an array after filtering and mapping by combining filter(), map(), and array manipulation techniques. Understanding the Approach The technique involves first filtering data based on a condition, then adding computed properties or objects derived from mapping operations on the original data. Example: Adding Property to Filtered Array const details = [ { customerName: 'John', customerCountryName: 'UK', isMarried: true }, { customerName: 'David', customerCountryName: 'AUS', isMarried: false }, { customerName: 'Mike', customerCountryName: 'US', isMarried: ...
Read More