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 155 of 589
Remove 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 MoreArray of multiples - JavaScript
We are required to write a JavaScript function that takes in two numbers, say m and n, and it returns an array of first n multiples of m. For example − If the numbers are 4 and 6 Then the output should be − const output = [4, 8, 12, 16, 20, 24] Example Following is the code − const num1 = 4; const num2 = 6; const multiples = (num1, num2) => { const res = []; for(let i = num1; i ...
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 MoreConcatenating variable number of arrays into one - JavaScript
We are required to write a JavaScript function that takes in any number of JavaScript arrays and returns one single array with all the values from input arrays concatenated into it. For example − If the input arrays are − [1, 5], [44, 67, 3], [2, 5], [7], [4], [3, 7], [6] Then the output should be − const output = [1, 5, 44, 67, 3, 2, 5, 7, 4, 3, 7, 6]; Using reduce() and concat() The most straightforward approach uses the reduce() method combined with concat() to merge ...
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 MoreFinding special array - JavaScript
An array is a special array if: - All the elements at odd indices are odd - All the elements at even indices are even We need to write a JavaScript function that takes an array and checks if it's a special array or not. Understanding the Logic For an array to be special: Index 0 (even): element must be even Index 1 (odd): element must be odd Index 2 (even): element must be even Index 3 (odd): element must be odd The key insight is that arr[i] % 2 should ...
Read MoreRemove any text not inside element tag on a web page with JavaScript?
To remove text nodes that are not inside HTML element tags, we use jQuery's contents() and filter() methods to identify text nodes, then remove() to delete them. Understanding Text Nodes vs Element Nodes In the DOM, there are different node types: Element nodes (nodeType = 1): HTML tags like , Text nodes (nodeType = 3): Plain text content not wrapped in tags Example HTML Structure Consider this HTML where we want to remove the unwrapped text: Demo Program This is also Demo Program The text "This is also Demo ...
Read MoreProduct of two number using HOC - JavaScript
Higher Order Functions (HOC) in JavaScript are functions that either receive another function as an argument or return a function as their result. When combined with closures, HOCs become a powerful tool for creating reusable and modular code. In this example, we'll create a Higher Order Function that calculates the product of two numbers using the concept of currying, where a function returns another function. Example Here's how to implement a product calculator using HOC: const num1 = 24; const num2 = 5; const productHOC = num1 => { return ...
Read MoreBetter ways to modify string with multiple methods using JavaScript
JavaScript provides several string methods that can be combined to modify and format strings effectively. You can use methods like toLowerCase(), toUpperCase(), charAt(), and slice() together to achieve proper string formatting. Let's say we have the following mixed-case string: var sentence = "tHIS iS tHE JavaScript pROGRAM"; console.log("Original:", sentence); Original: tHIS iS tHE JavaScript pROGRAM Method 1: Using charAt() and slice() This approach capitalizes the first letter and converts the rest to lowercase: var sentence = "tHIS iS tHE JavaScript pROGRAM"; function modifyStringWithMultipleMethods(sentence) { ...
Read More