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
Javascript Articles
Page 470 of 534
Assigning function to variable in JavaScript?
In JavaScript, you can assign a function to a variable in several ways. This allows you to store functions for later use, pass them as arguments, or return them from other functions. Method 1: Function Expression The most common way is using a function expression, where you assign an anonymous function to a variable: Function Assignment // Assigning function to variable ...
Read MoreGet the last item from node list without using length property in JavaScript?
In JavaScript, you can get the last item from a NodeList without using the length property by leveraging CSS selectors, array methods, or destructuring. This is useful when you want to avoid calculating the length explicitly. Sample HTML Structure Let's use the following table as our example: John David Mike Method 1: Using CSS :last-child Selector The most direct approach is using CSS selectors to target the last element: ...
Read MoreAssigning values to a computed property in JavaScript?
In JavaScript, computed properties allow you to create object properties with dynamic names. This article shows how to extract data from an array of objects and assign values to computed properties using array methods. Sample Data Let's start with an array of customer details objects: const customerDetails = [ {customerFirstName: "David"}, {customerLastName: "Miller"}, {customerCountryName: "US"}, {customerAge: "29"}, {isMarried: false}, {customerCollegeName: null} ]; console.log("Original data:", customerDetails); Original data: [ ...
Read MoreGenerate random characters and numbers in JavaScript?
Generating random characters and numbers is a common requirement in JavaScript applications. You can create random strings by combining characters from a predefined set using Math.random(). Setting Up Character Pool First, define a string containing all possible characters you want to include: var storedCharacters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; Basic Random String Generator Here's a function that generates random strings of any specified length: function generateRandomString(size) { var generatedOutput = ''; var storedCharacters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; var totalCharacterSize = storedCharacters.length; ...
Read MoreTrigger a button click and generate alert on form submission in JavaScript
In JavaScript, you can trigger a button click event and generate an alert on form submission using event handlers. This tutorial demonstrates how to attach a click event listener to a button and display an alert when the form is submitted. HTML Structure First, let's create a basic HTML form with a button: Submit JavaScript Event Handler Using jQuery, you can attach a click event handler to the button based on its ID: $("#submitForm").click(function() { alert("The Form has been Submitted."); }); Complete Example Here's ...
Read MoreHow to find the second largest element in a user-input JavaScript array?
Finding the second largest element in a JavaScript array requires comparing elements and tracking the two highest values. Here are several approaches to accomplish this task. Method 1: Using Single Pass Iteration This approach iterates through the array once, maintaining variables for the largest and second largest elements: var numbers = [10, 50, 80, 60, 89]; var firstLargerNumber = Number.MIN_SAFE_INTEGER; var secondlargerNumber = firstLargerNumber; for(var tempNumber of numbers){ if(tempNumber > firstLargerNumber){ secondlargerNumber = firstLargerNumber; firstLargerNumber ...
Read MoreToggle hide class only on selected div with JavaScript?
To toggle hide class only on selected div, you need to set event on click button. Let's say you need to hide a specific div on the click of + sign. To get font + or - icon, you need to link font-awesome: HTML Structure The HTML contains multiple customer sections, each with a toggle button that can show/hide specific content: Toggle Hide Class Example ...
Read MoreFind the number of times a value of an object property occurs in an array with JavaScript?
When working with arrays of objects, you often need to count how many times a specific property value appears. JavaScript's reduce() method combined with Map provides an efficient solution for this task. Using reduce() with Map The reduce() method processes each array element to build a frequency count. We use a Map to store the counts because it maintains insertion order and provides efficient lookups. const subjectDetails = [ { subjectId: '101', subjectName: 'JavaScript' ...
Read MoreGetting HTML form values and display on console in JavaScript?
In JavaScript, you can retrieve HTML form values using the value property of form elements. This is essential for processing user input and form data. Basic Syntax To get a form element's value, use: document.getElementById("elementId").value Example: Getting Input Value Here's how to get a text input value and display it in the console: Get Form Values ...
Read MorePrint JSON nested object in JavaScript?
To print JSON nested objects in JavaScript, you can use various approaches depending on the structure of your data. When dealing with nested JSON strings within objects, you'll need to parse them first using JSON.parse(). Example: Parsing Nested JSON Strings Here's how to handle objects containing JSON strings as properties: var details = [ { "studentId": 101, "studentName": "John", "countryName": "US", ...
Read More