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 328 of 534
How to set the padding of an element with JavaScript?
In this tutorial, we will look at the methods to set the padding of an element with JavaScript. Padding can be termed as the space between an element's content and its border, whereas margin adds space around the edge. The padding property in JavaScript allows you to control this internal spacing dynamically. Understanding CSS Padding Values Before diving into JavaScript implementation, let's understand how padding values work: padding: 10px; Sets 10px padding to all four sides (top, right, bottom, left) padding: 10px 20px; Sets 10px to top/bottom, 20px to ...
Read MoreHow to set the left padding of an element with JavaScript?
Use the paddingLeft property in JavaScript to set the left padding of an element. This property allows you to dynamically modify the spacing between an element's content and its left border. Syntax element.style.paddingLeft = "value"; The value can be specified in pixels (px), percentages (%), em units, or other CSS length units. Example #box { border: 2px solid ...
Read MoreSum is to be calculated for the numbers in between the array's max and min value JavaScript
We need to write a function called sumBetween() that takes an array of two elements and returns the sum of all integers between those values, including both endpoints. For example: [4, 7] = 4+5+6+7 = 22 [10, 6] = 10+9+8+7+6 = 40 Understanding the Problem The function should work regardless of which number is larger. Whether we pass [4, 7] or [7, 4], we need to sum all integers from 4 to 7 inclusive. Solution Using Mathematical Formula We can use the mathematical formula for sum of consecutive integers: sum from 1 ...
Read MoreHow to set the horizontal alignment of text with JavaScript?
The textAlign property in JavaScript controls the horizontal alignment of text within an element. You can set it to values like left, right, center, or justify. Syntax element.style.textAlign = "alignment_value"; Available Alignment Values left - Aligns text to the left (default) right - Aligns text to the right center - Centers the text justify - Stretches text to fill the line width Example: Setting Right Alignment ...
Read MoreGet the item that appears the most times in an array JavaScript
Let's say we are required to write a function that takes in an array of string/number literals and returns the index of the item that appears for the most number of times. We will iterate over the array and prepare a frequency map, and from that map we will return the index that makes the most appearances. Example const arr1 = [12, 5, 6, 76, 23, 12, 34, 5, 23, 34, 65, 34, 22, 67, 34]; const arr2 = [12, 5, 6, 76, 23, 12, 34, 5, 23, 34]; const mostAppearances = (arr) => { ...
Read MoreHow to set how the last line of a block or a line right before a forced line break is aligned when text-align is "justify" with JavaScript?
The textAlignLast property in JavaScript controls how the last line of a text block is aligned when the text uses text-align: justify. This is particularly useful for controlling the alignment of the final line in justified paragraphs. Syntax element.style.textAlignLast = "value"; Common Values The textAlignLast property accepts these values: auto - Default behavior (usually left-aligned) left - Aligns last line to the left right - Aligns last line to the right center - Centers the last line justify - Justifies the last line (stretches it) Example: Setting Last Line Alignment ...
Read MoreHow to slice an array with wrapping in JavaScript
Let's say, we are required to write an array method that overwrites the default Array.prototype.slice(). Usually the Array.prototype.slice() method takes in two arguments the start index and the end index, and returns a subarray of the original array from index start to end-1. What we wish to do is make this slice() function so it returns a subarray from index start to end and not end-1. We iterate over the array using a for loop which is faster than many array methods. Then return the required subarray, lastly we overwrite the Array.prototype.slice() with the method we just wrote. ...
Read MoreHow to set the decoration of a text with JavaScript?
Use the textDecoration property in JavaScript to decorate text. This property allows you to add underlines, overlines, strikethrough effects, and more to any text element. Syntax element.style.textDecoration = "value"; Common textDecoration Values Value Effect underline Adds line below text overline Adds line above text line-through Strikes through text none Removes decoration Example: Basic Text Decoration Text Decoration Example This is demo text. ...
Read MoreHow to reduce an array while merging one of its field as well in JavaScript
Consider, we have the following array of objects − const arr = [{ id: 121, hobby: 'cycling' }, { id: 125, hobby: 'jogging' }, { id: 129, hobby: 'reading' }, { id: 121, hobby: 'writing' }, { id: 121, hobby: 'playing football' }, { id: 125, hobby: 'cooking' }, { id: 129, hobby: 'horse riding' }]; ...
Read MoreHow to set the color of the text-decoration with JavaScript?
In this tutorial, we will learn how to set the color of the text-decoration with JavaScript. The text-decoration is a CSS property used to decorate text lines. We can decorate a line using underline, overline, line-through, or none. To set the color of the text-decoration with JavaScript, we have multiple ways, and in this tutorial, we will discuss two of them − Using the style.textDecorationColor Property Using the style.setProperty Method Using the style.textDecorationColor Property In JavaScript, the style.textDecorationColor property of an element is used to set the color ...
Read More