Javascript Articles

Page 464 of 534

Remove any text not inside element tag on a web page with JavaScript?

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

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 More

Better ways to modify string with multiple methods using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 419 Views

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

How do I run two or more functions when using 'onclick' JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 634 Views

When handling click events in JavaScript, you often need to execute multiple functions. There are several ways to run two or more functions when using the onclick event. Method 1: Using a Wrapper Function Create a single function that calls multiple functions inside it: Multiple Functions onClick Call Functions function callTwoFunctions() { ...

Read More

Detect the ENTER key in a text input field with JavaScript

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

Detecting the ENTER key in a text input field is a common requirement in web development. You can accomplish this using JavaScript event listeners and checking for the specific key code or key value. HTML Setup First, let's create a simple text input field: Method 1: Using keyCode (Legacy) The traditional approach uses keyCode property with value 13 for the ENTER key: ENTER Key Detection ...

Read More

JavaScript recursive loop to sum all integers from nested array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 467 Views

JavaScript recursive functions can process nested arrays by calling themselves repeatedly to handle arrays within arrays. This technique is essential for working with complex data structures of unknown depth. Example: Basic Recursive Sum function sumOfTotalArray(numberArray) { var total = 0; for (var index = 0; index < numberArray.length; index++) { if (numberArray[index] instanceof Array) { total = total + sumOfTotalArray(numberArray[index]); } ...

Read More

JavaScript filter an array of strings, matching case insensitive substring?

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

To filter an array of strings with case-insensitive matching, use JavaScript's filter() method combined with toLowerCase() and indexOf(). Setting Up the Data Let's start with an array of student objects containing names with different cases: let studentDetails = [ {studentName: "John Smith"}, {studentName: "john smith"}, {studentName: "Carol Taylor"}, {studentName: "JOHN TAYLOR"}, {studentName: "alice johnson"} ]; console.log("Original array:", studentDetails); Original array: [ { studentName: 'John Smith' }, { studentName: ...

Read More

Check whether Enter key is pressed or not and display the result in console with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 374 Views

In JavaScript, you can detect when the Enter key is pressed using the onkeypress event handler. This is useful for form submissions, search functionality, or triggering actions when users press Enter. Key Code for Enter Key The Enter key has a key code of 13. We can check this using the keyCode property of the event object. Basic Implementation First, create an input field with an onkeypress event handler: Then, create a function to detect the Enter key: function checkEnterKey(event) { if (event.keyCode == 13) ...

Read More

Replace a value if null or undefined in JavaScript?

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

In JavaScript, you can replace null or undefined values using the logical OR operator (||) or the nullish coalescing operator (??). Syntax // Using logical OR operator var result = value || defaultValue; // Using nullish coalescing operator (ES2020+) var result = value ?? defaultValue; Using Logical OR Operator (||) The || operator returns the first truthy value or the last value if all are falsy: var value = null; console.log("The original value:", value); var actualValue = value || "This is the Correct Value"; console.log("The replaced value:", actualValue); // ...

Read More

Is it possible to select text boxes with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 785 Views

Yes, it's possible to select text boxes with JavaScript using the select() method. This method highlights all text within an input field, making it ready for user interaction or replacement. The select() Method The select() method programmatically selects all text content in an input field. It's commonly used to improve user experience by pre-selecting text for easy editing or copying. Basic Syntax element.select(); Example: Selecting Text on Button Click Select Text Box Example ...

Read More

JavaScript (+) sign concatenates instead of giving sum?

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

The + sign concatenates strings instead of adding numbers when JavaScript treats the values as strings. This commonly happens with form inputs, which always return string values even when they contain numbers. The Problem: String Concatenation vs Addition When you use the + operator with strings, JavaScript concatenates them instead of performing mathematical addition: String Concatenation Problem // These are strings, not numbers var a = ...

Read More
Showing 4631–4640 of 5,340 articles
« Prev 1 462 463 464 465 466 534 Next »
Advertisements