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
Articles by AmitDiwan
Page 454 of 840
How would I add a newline in an Unordered List (UL) from JavaScript?
To add a new line (list item) to an Unordered List in JavaScript, use document.querySelector().append() or appendChild(). This allows you to dynamically add elements to an existing . Basic Syntax // Create a new list item let listItem = document.createElement('li'); listItem.textContent = 'Your content here'; // Append to the ul document.querySelector('ul').append(listItem); Complete Example Add Items to List Demo h1 { ...
Read MoreImplement Onclick in JavaScript and allow web browser to go back to previous page?
JavaScript provides several methods to implement browser navigation functionality. The most common approach is using window.history.go(-1) with an onclick event to go back to the previous page. Syntax window.history.go(-1); // Go back one page window.history.back(); // Alternative method Using window.history.go(-1) The window.history.go(-1) method navigates back one page in the browser history: Go Back Example Current Page ...
Read MoreCreating auto-resize text area using JavaScript
In this post, we are going to learn how to set up an auto-resize text area in JavaScript. This text area will be able to change its height automatically depending on the content within it. Auto-resize text in JavaScript is a helpful feature that can be used to edit multiple lines of text. The text can automatically change its height depending on the content inside it. The default behavior of textarea doesn't allow this, but we can create it by using the input event listener over it. Let's understand how to adjust the height of the text area element ...
Read MoreHow to transform JavaScript arrays using maps?
JavaScript's map() method creates a new array by transforming each element of the original array using a callback function. It's essential for functional programming and data transformation. Syntax array.map(callback(element, index, array)) Parameters callback - Function that transforms each element element - Current array element being processed index - Index of the current element (optional) array - The original array (optional) Basic Example: Squaring Numbers Transform Arrays with Map ...
Read MoreHow to get only the first BOT ID from thes JavaScript array?
When working with an array of objects in JavaScript, you often need to access specific properties from the first element. Let's explore how to get the first BOT ID from an array of user records. Sample Data Structure Consider an array of objects where each object contains a BOTID and Name: let objectArray = [ { BOTID: "56", Name: "John" }, { BOTID: "57", Name: "David" }, { BOTID: "58", Name: "Sam"}, { BOTID: "59", Name: "Mike" }, ...
Read MoreCalculating excluded average - JavaScript
In JavaScript, you may need to calculate the average of specific values in an array of objects based on certain conditions. This tutorial shows how to compute the average of values where a boolean flag indicates they should be included in the calculation. Problem Description Given an array of objects with val and canUse properties, we need to calculate the average of val values only for objects where canUse is true. const arr = [ {val: 56, canUse: true}, {val: 16, canUse: true}, {val: 45, canUse: true}, ...
Read MoreHow to toggle a boolean using JavaScript?
In this tutorial, we will learn how to toggle a boolean value in JavaScript. In addition to this, we will also learn how we can use it for various web application features that require toggling a certain piece of state or value. Toggling a boolean in JavaScript entails altering its value from true to false or vice versa. Toggling booleans can be helpful in a variety of situations, such as changing a boolean value to initiate an action or to toggle between states or the visibility of an element. Toggling can be implemented using different methods like the ...
Read MoreGetting an HTML H1 value to JavaScript variable?
To get the value of an HTML H1 element into a JavaScript variable, you can use document.getElementById().innerHTML to access the text content inside the heading. Basic Syntax var headingText = document.getElementById('elementId').innerHTML; Example: Getting H1 Content Let's say we have the following H1 heading with an id: This is the demo program of JavaScript Here's how to get the H1 value using JavaScript: Getting H1 Value ...
Read MoreHow Do I Find the Largest Number in a 3D JavaScript Array?
To find the largest number in a 3D JavaScript array, we can use flat(Infinity) to flatten all nested levels, then apply Math.max() to find the maximum value. The Problem 3D arrays contain multiple levels of nested arrays, making it difficult to directly compare all numbers. We need to flatten the structure first. var theValuesIn3DArray = [75, [18, 89], [56, [97], [99]]]; Using flat() with Math.max() The flat(Infinity) method flattens all nested levels, and the spread operator ... passes individual elements to Math.max(). var theValuesIn3DArray = [75, [18, 89], [56, [97], [99]]]; ...
Read MoreResolving or rejecting promises accordingly - JavaScript
We are required to write a JavaScript function that imitates a network request, for that we can use the JavaScript setTimeout() function, that executes a task after a given time interval. Our function should return a promise that resolves when the request takes place successfully, otherwise it rejects Understanding Promise Resolution and Rejection When creating promises, we use the Promise constructor which takes an executor function with two parameters: resolve and reject. The resolve function is called when the operation succeeds, while reject is called when it fails. Example Following is the code that demonstrates ...
Read More