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 111 of 534
How can I show a hidden div when a select option is selected in JavaScript?
Showing and hiding elements dynamically is a fundamental technique in JavaScript web development. One common scenario is revealing a hidden div when a specific select option is chosen, creating interactive forms and user interfaces. Methods for Showing/Hiding Elements There are two main CSS properties for controlling element visibility: display: Removes element from document flow when hidden visibility: Keeps element's space but makes it invisible Method 1: Using visibility Property The visibility property hides the element while preserving its layout space: ...
Read MoreReplace whitespace in different elements with the same class in JavaScript?
In this article we are going to learn about replacing whitespace in different elements with the same class in JavaScript. We'll explore multiple methods to remove spaces from text content across multiple HTML elements that share a common CSS class. Using split() and join() Methods The split() method divides a string into substrings and returns them as an array. When a separator is specified, the string is split at each occurrence of that separator. The join() method combines array elements back into a single string using a specified separator. Syntax string.split(" ").join("") Example ...
Read MoreUse jQuery to get values of selected checkboxes?
jQuery provides an efficient way to get values of selected checkboxes using the :checked selector combined with checkbox input elements. This is useful for forms where users can select multiple options. Syntax $('input[type="checkbox"]:checked') // or $('input[name="checkboxName"]:checked') Basic Example Here's a complete example that demonstrates getting values of selected checkboxes: jQuery Checkbox Values Select your hobbies: ...
Read MoreJavaScript subtraction of two float values?
JavaScript floating-point arithmetic can produce unexpected results due to precision limitations. To correctly subtract two float values and get accurate results, use parseFloat() to ensure proper number conversion and toFixed() to control decimal precision. The Problem with Float Subtraction Direct subtraction of floats can yield imprecise results: var result1 = 0.3 - 0.1; var result2 = 0.2 - 0.1; document.write("0.3 - 0.1 = " + result1 + ""); ...
Read MoreHow to stop the loop for JavaScript scroll down?
JavaScript scroll down loops can be extremely annoying and disruptive to a web page. Fortunately, it is relatively easy to stop the loop if you know what steps to take. In this article, we will discuss how to stop the JavaScript scroll down loop so that your website visitors don't have to keep scrolling endlessly. We'll also provide some tips on preventing these types of issues in the future. JavaScript method scrollBy() scrolls the web page to the specific number of pixels. When used in loops or intervals, it can create continuous scrolling effects that need proper control mechanisms. ...
Read MoreHow to make a setInterval() stop after some time or after a number of actions in JavaScript?
The setInterval() function repeatedly executes code at specified intervals. To stop it after some time or a number of actions, use clearInterval() with conditions. Method 1: Stop After Time Duration This example stops the interval after 10 seconds: Stop setInterval After Time var startTime = new Date().getTime(); ...
Read MoreArray.prototype.fill() with object passes reference and not new instance in JavaScript?
When using Array.prototype.fill() with objects, JavaScript passes the same reference to all array positions instead of creating new instances. This means modifying one object affects all elements. The Problem with fill() and Objects Using fill() with an object creates multiple references to the same object: // This creates the same object reference in all positions let arr = new Array(3).fill({name: "John", age: 25}); console.log("Original array:"); console.log(arr); // Modifying one element affects all arr[0].name = "Alice"; console.log("After modifying arr[0].name:"); console.log(arr); Original array: [ { name: 'John', age: 25 }, { name: ...
Read MoreShortest syntax to assign properties from function call to an existing object in JavaScript
In JavaScript, you can quickly assign properties from a function call to an existing object using several concise syntaxes. This article explores the most efficient approaches to merge function-returned properties into existing objects. An object in JavaScript is a separate entity having properties and a type. JavaScript objects can have properties that specify their attributes. Using Spread Syntax (...) The spread syntax provides the shortest way to assign properties from a function call to an existing object. It expands an object's properties into a new object, effectively merging them. Syntax existingObject = { ...existingObject, ...
Read MoreIs there any way to check if there is a null value in an object or array in JavaScript?
Null values can be tricky to work with in JavaScript, but they are an important part of the language. In this article, we will discuss various ways you can check if there is a null value in an object or array in JavaScript. The null values display that no object value is present. It is intentionally set to show that a variable has been declared but has not yet been given a value. The primitive value is undefined, which is an unintended absence of any object value, which is comparable to null, that contrasts with the former. This ...
Read MoreAdd class name in different li by pure in JavaScript?
Adding class names to different list items (li elements) is a common task in JavaScript DOM manipulation. This can be achieved using various methods like forEach() with classList.add(), CSS selectors, or DOM traversal properties. Let's explore different approaches to add class names to list items using pure JavaScript. Method 1: Using forEach() with classList.add() The forEach() method executes a function for each array element, while classList.add() adds one or more CSS classes to an element. Syntax array.forEach(function(currentValue, index, arr), thisValue) element.classList.add('className') Example ...
Read More