AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 413 of 840

How to sort an HTML list using JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

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 More

When you should not use JavaScript Arrow Functions?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 226 Views

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 More

Extract properties from an object in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 3K+ Views

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 More

Search and update array based on key JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 240 Views

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 More

Make HTML text input field grow as I type in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 549 Views

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 More

Move string capital letters to front maintaining the relative order - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 374 Views

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 More

How to sort an HTML table using JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

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 More

How can I disable JavaScript click function if link starts with #?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 478 Views

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 More

Using '{ }' in JavaScript imports?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 206 Views

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 More

Loop backward in array of objects JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 211 Views

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
Showing 4121–4130 of 8,392 articles
« Prev 1 411 412 413 414 415 840 Next »
Advertisements