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 413 of 840
How to sort an HTML list using JavaScript?
Sorting an HTML list with JavaScript involves manipulating the DOM elements to reorder list items based on their content. Here are two effective approaches to accomplish this. Method 1: Using Array Methods (Recommended) This modern approach converts list items to an array, sorts them, and rebuilds the list: Sort HTML List Animal List Sorting Sort Alphabetically Zebra Elephant ...
Read MoreWhen you should not use JavaScript Arrow Functions?
Arrow functions should not be used in certain scenarios because they don't have their own this binding. Instead, they inherit this from the enclosing lexical scope, which can lead to unexpected behavior. When NOT to Use Arrow Functions Object Methods Arrow functions should not be used as object methods because they don't bind this to the object. Instead, this refers to the global scope (window in browsers). Arrow Functions - Object Methods Arrow Function vs Regular Function in Objects ...
Read MoreExtract properties from an object in JavaScript
We have to write a JavaScript function, say extract() that extracts properties from an object to another object and then deletes them from the original object. For example − If obj1 and obj2 are two objects, then obj1 = {color:"red", age:"23", name:"cindy"} obj2 = extract(obj1, ["color", "name"]) After passing through the extract function, they should become like − obj1 = { age:23 } obj2 = {color:"red", name:"cindy"} Therefore, let's write the code for this function − Syntax const extract = (obj, ...keys) => { ...
Read MoreSearch and update array based on key JavaScript
In JavaScript, you often need to merge data from two arrays based on a common property. This is useful when you have reference data in one array and need to enrich objects in another array. Consider these two arrays: let arr1 = [ {"LEVEL":4, "POSITION":"RGM"}, {"LEVEL":5, "POSITION":"GM"}, {"LEVEL":5, "POSITION":"GMH"} ]; let arr2 = [ {"EMAIL":"test1@stc.com", "POSITION":"GM"}, {"EMAIL":"test2@stc.com", "POSITION":"GMH"}, {"EMAIL":"test3@stc.com", "POSITION":"RGM"}, {"EMAIL":"test3@CSR.COM.AU", "POSITION":"GM"} ]; console.log("Original arr2:", arr2); ...
Read MoreMake 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 MoreMove string capital letters to front maintaining the relative order - JavaScript
We need to write a JavaScript function that takes a string with uppercase and lowercase letters and returns a new string with all uppercase letters moved to the front while maintaining their relative order. For example, if the input string is: const str = 'heLLO woRlD'; Then the output should be: const output = 'LLORDhe wol'; Algorithm Explanation The approach uses an array and a tracking index. We iterate through each character, and if it's uppercase, we insert it at the current capital position and increment the index. Lowercase characters ...
Read MoreHow to sort an HTML table using JavaScript?
Sorting HTML tables using JavaScript allows for dynamic data organization without server requests. This technique uses DOM manipulation to reorder table rows based on cell content. How Table Sorting Works The sorting algorithm compares adjacent rows and swaps them if they're in the wrong order. This bubble sort approach continues until all rows are properly sorted alphabetically. Ron Shawn Caleb Compare After ...
Read MoreHow can I disable JavaScript click function if link starts with #?
In JavaScript, you can disable click functionality for links that start with "#" using preventDefault() and CSS selectors. This technique prevents hash links from executing click handlers while allowing normal links to work. Problem Sometimes you need to apply click handlers to most links but exclude hash links (href="#") that are used for navigation or placeholder purposes. Solution Using jQuery Use the :not() selector to exclude links with href="#" from the click handler: Disable Hash Link Clicks ...
Read MoreUsing '{ }' in JavaScript imports?
In JavaScript ES6 modules, curly braces { } are used for named imports, allowing you to import specific exported functions, variables, or classes from a module. Syntax // Named imports import { functionName, variableName } from './module.js'; // Named imports with alias import { originalName as newName } from './module.js'; // Mixed import import defaultExport, { namedExport } from './module.js'; Example JavaScript Named Imports ...
Read MoreLoop backward in array of objects JavaScript
When working with arrays of objects in JavaScript, you may need to iterate through them in reverse order. This is useful for tasks like displaying data from newest to oldest or building strings that reverse the original order. We have an array of objects like this: let data = [ {id:1, Name: "Abe", RowNumber: 1 }, {id:2, Name: "Bob", RowNumber: 2 }, {id:3, Name: "Clair", RowNumber: 3 }, {id:4, Name: "Don", RowNumber: 3.0 }, {id:5, Name: "Edna", ...
Read More