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 463 of 534
How 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 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 MoreHow to transform object of objects to object of array of objects with JavaScript?
To transform an object of objects into an object of array of objects, you can use Object.fromEntries() along with Object.entries() and map(). This transformation wraps each nested object in an array while preserving the original keys. Syntax Object.fromEntries( Object.entries(originalObject).map(([key, value]) => [key, [value]]) ) Example const studentDetails = { 'details1': {Name: "John", CountryName: "US"}, 'details2': {Name: "David", CountryName: "AUS"}, 'details3': {Name: "Bob", CountryName: "UK"}, }; const transformedObject = Object.fromEntries( Object.entries(studentDetails).map(([key, value]) => ...
Read MoreIs an empty iframe src valid in JavaScript?
An empty iframe src can be handled in JavaScript using about:blank or by leaving the src attribute empty. The about:blank approach is more reliable and creates a valid, blank document. Valid Approaches for Empty iframe src There are several ways to create an empty iframe: Using about:blank (Recommended) The about:blank value creates a valid empty document that can be safely manipulated with JavaScript: Empty iframe Example ...
Read MoreLooping in JavaScript to count non-null and non-empty values
In JavaScript, counting non-null and non-empty values in an array is a common task. This article demonstrates how to use forEach() to iterate through an array and count valid values. Basic Array Example Let's start with an array containing subject names: let subjectNames = ['JavaScript', 'Angular', 'AngularJS', 'Java']; console.log("Original array:", subjectNames); Original array: [ 'JavaScript', 'Angular', 'AngularJS', 'Java' ] Using forEach() to Count Valid Values The forEach() method executes a function for each array element. Here's the syntax: yourArrayName.forEach(element => { // Your logic ...
Read MoreRemove and add new HTML Tags with JavaScript?
JavaScript provides several methods to dynamically remove and add HTML elements to the DOM. You can hide/show existing elements or completely remove/create new elements using vanilla JavaScript or jQuery. Method 1: Using jQuery hide() and show() The simplest approach is using jQuery's hide() and show() methods to toggle element visibility: Hide/Show Elements Test JavaScript This content can be hidden and shown. ...
Read MoreHow can I instantiate a dictionary in JavaScript where all keys map to the same value?
In JavaScript, you can create a dictionary (object) where all keys map to the same value using several approaches. This is useful when you need to initialize multiple properties with identical values. Method 1: Using a for...of Loop The most straightforward approach is to iterate through an array of keys and assign the same value to each: const keys = ['Name1', 'Name2', 'Name3']; const dictionary = {}; for (const key of keys) { dictionary[key] = 'John'; } console.log(dictionary); { Name1: 'John', Name2: 'John', Name3: 'John' } ...
Read MoreHow 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 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 More