JavaScript

The primary use of JavaScript is to write functions that are embedded in or included from HTML pages and that interact with the Document Object Model (DOM) of the page.

JavaScript

Order objects in array by property in JavaScript

12 Jun, 2024
In JavaScript, you can sort arrays of objects by a specified property using custom functions that handle both string and integer properties, and can sort in either ascending or descending order. The...
JavaScript

Vanilla JavaScript trigger click event

24 Feb, 2024
To trigger a click event using vanilla JavaScript, you can use the click() method on the element you want to simulate a click for. For more information: https://developer.mozilla.org/en-US/docs/Web/...
JavaScript

Iterate objects in JavaScript

21 Feb, 2024
In JavaScript, for iterating over objects, I recommend using the Object.entries() method, introduced in ECMAScript 2017 (ES8) and supported by modern browsers. This method provides key-value pairs in...
JavaScript

Deep merge in JavaScript

21 Feb, 2024
Deep merging in JavaScript refers to the process of merging two or more objects deeply, ensuring that nested structures, including arrays, are merged recursively. While JavaScript provides the spread...
JavaScript

Remove all but first n array items in JavaScript

10 Feb, 2024
To remove all but the first n elements from an array in JavaScript, you can use the splice() method or array slicing. The splice() method in JavaScript is used to change the contents of an array by...
JavaScript

JavaScript one-liner to get unique array values

9 Feb, 2024
This snippet uses a combination of Set and Array.from() to filter out duplicate values and convert the result back into an array. When you initialize a Set with an array, it automatically removes any...
JavaScript

JavaScript: Get index by value in array

9 Feb, 2024
In JavaScript, if you want to get the index of an element in an array based on its value, you can use the indexOf() method. If the value is not found in the array, the indexOf() method will return -1...
JavaScript

Element scroll offset in JavaScript

7 Feb, 2024
This code snippet demonstrates how to retrieve the horizontal (x) and vertical (y) scroll coordinates of an element using the scrollLeft and scrollTop properties, respectively. One common use case...
JavaScript

JavaScript: Move all elements from a div to another div

7 Feb, 2024
In this snippet we move/clone dom elements with all children for later usage.
JavaScript

Get last item of array in JavaScript

31 Jan, 2024
This snippet explains how to get last item of an array. The example retrieves the last item of the array by determining its index through subtracting one from the array's length. This approach...
JavaScript

Vanilla JavaScript equivalent of jQuery addClass

20 Jan, 2024
The addClass function in jQuery and the classList.add method in vanilla JavaScript both serve the purpose of adding one or more CSS classes to HTML elements. jQuery's addClass is succinct and...
JavaScript

Vanilla JavaScript equivalent of jQuery show/hide

20 Jan, 2024
To achieve a show/hide functionality in vanilla JavaScript equivalent to jQuery's show() and hide() functions, you can utilize CSS classes or inline styles. In the example provided, a CSS class named...
JavaScript

Vanilla JavaScript equivalent of jQuery selector

20 Jan, 2024
The difference between querySelectorAll in vanilla JavaScript and jQuery's selector lies in their syntax and underlying implementation. querySelectorAll is a native JavaScript method that allows for...
JavaScript

JavaScript: recur every minute

20 Jan, 2024
If you want to make a function or a piece of code in JavaScript execute every minute, you can use the setInterval function. The setInterval function in JavaScript is used to repeatedly execute a...
JavaScript

JavaScript: get group that contains item in a grouped array

15 Jan, 2024
You have an original array and another array where the elements are grouped in threes. You want to find the group in the second array that corresponds to a given element from the original array.
JavaScript

Split array to groups of 3 in JavaScript

15 Jan, 2024
Split array to groups of 3. For convenience there are two methods. Both variations achieve the task of splitting an array into groups of three elements in JavaScript. The first variation uses a more...
JavaScript

Redirect to another page with JavaScript

11 Jan, 2024
JavaScript redirection, exploring the different methods and scenarios where it can be applied. Whether you prefer a silent redirect or a documented exploration of pages, these snippets provide the...
JavaScript

Parse an HTML string with JavaScript

11 Jan, 2024
DOMParser API provides a more flexible way to parse HTML strings into a document object, facilitating efficient manipulation and extraction of information within a web page. After which you can use...
JavaScript

Get attribute value from getElementsByTagName()

11 Jan, 2024
To retrieve the value of an attribute using getElementsByTagName() in JavaScript, you first use the document.getElementsByTagName("tag") method, replacing "tag" with the HTML tag of the elements you'...
JavaScript

Last element of array in JavaScript

2 Jan, 2024
Accessing the last element of an array in JavaScript, as demonstrated by the code snippet, is a fundamental technique for practical programming tasks. It allows you to retrieve and utilize the latest...
JavaScript

Count object keys in javascript

23 Nov, 2023
In JavaScript, you can count the number of keys in an object using the Object.keys() method and then .length is used to get the number of keys in the object.
JavaScript

JavaScript event when pressing escape key

7 Nov, 2023
Capturing the "Escape" key press with an event listener is a common practice in web development, especially for modals or dialogs. Example useful cases of using the event: It provides a better user...
JavaScript

Three dots add objects together (spread operator)

19 Oct, 2023
You can use the spread operator (...) to add objects or merge them together. This operator allows you to copy the properties of one object into another. The code always creates a new object. In this...
JavaScript

Get body tag/element in JavaScript

7 Oct, 2023
You don't have to find the body by tag name. This code will give you a reference to the element of the current HTML document, which you can then manipulate or access as needed in your JavaScript...
JavaScript

isset() equivalent in JavaScript

7 Oct, 2023
In JavaScript, there isn't a built-in isset() function like you might find in some other programming languages. However, you can check if a variable is defined or not by using conditional statement.
JavaScript

CKEditor 4 allowedContent link

5 Aug, 2023
The allowedContent configuration option in CKEditor is used to define what HTML elements and attributes are allowed to be used in the editor's content. This feature helps maintain consistent and safe...
JavaScript

Use keydown instead of keyup to prevent scrolling with space-bar

25 Jun, 2023
To prevent scrolling with the spacebar using the keydown event instead of keyup, you can listen for the spacebar key using JavaScript and prevent the default behavior of the key if it is pressed down...
JavaScript

Vanilla JavaScript equivalent of jQuery :visible

25 Jun, 2023
In jQuery, the :visible selector is used to select elements that are currently visible on the web page. The code snippet checks if an element has a non-zero width, a non-zero height, or non-zero...
JavaScript

Vanilla JavaScript equivalent of jQuery each

14 May, 2023
In jQuery, you can use the $.each() method to loop through a collection of elements or objects. In vanilla JavaScript, you can achieve the same functionality using the forEach() method, which is...
JavaScript

Vanilla JavaScript equivalent of jQuery parent

14 May, 2023
To use these, you first need to get a reference to the child element whose parent you want to get. You can use a method such as getElementById(), querySelector(), or getElementsByClassName() to get a...
JavaScript

Vanilla JavaScript equivalent of jQuery hasClass

14 May, 2023
Both the native JavaScript and jQuery methods achieve the same result of checking whether an element has a particular class. However, the syntax and implementation are different between the two. In...
JavaScript

Get current focused element in JavaScript

14 May, 2023
When developing a webpage or web application, it can be useful to know which element is currently in focus. This information can be used to provide visual feedback or to perform actions based on the...
JavaScript

Vanilla JavaScript equivalent of jQuery text

17 Feb, 2023
The Vanilla JS equivalent of jQuery's text() method would be to set the textContent property of a DOM element.
JavaScript

Vanilla JavaScript equivalent of jQuery css

17 Feb, 2023
In jQuery, you can set CSS properties on an element using the css() method. To achieve the same thing in vanilla JavaScript, you can use the style property of the element.
JavaScript

Vanilla JavaScript equivalent of jQuery attr

17 Feb, 2023
In jQuery, the attr method is used to get or set the value of an attribute for an element. In vanilla JavaScript, you can use the getAttribute and setAttribute methods to achieve the same...
JavaScript

Vanilla JavaScript equivalent of jQuery click

13 Jan, 2023
In vanilla JavaScript, the equivalent of the jQuery click function would be the onclick property. The onclick property in JavaScript is an event property of HTML elements that allows you to specify a...
JavaScript

in_array equivalent in JavaScript

12 Jan, 2023
In JavaScript, the equivalent of the PHP function in_array() is the includes() method, which is used to check if an array contains a certain value.
JavaScript

Copy text to clipboard in JavaScript

2 Jan, 2023
Certainly! Copying text to the clipboard in JavaScript can be accomplished using the Clipboard API. This API allows web developers to interact with the user's clipboard, allowing them to read from...
JavaScript

Vanilla JS equivalent of jQuery prepend

2 Jan, 2023
Insert content to the beginning of an element. Vanilla JS equivalent prepends to a single element, to prepend to multiple elements you need a loop. Same applies to append.
JavaScript

Vanilla JS equivalent of jQuery find

30 Dec, 2022
The Vanilla JavaScript equivalent of jQuery's .find() method is the .querySelectorAll() method. The function allows us to search through the descendants of these elements in the DOM tree and...
JavaScript

Javascript: object keys and values to html table

14 Jun, 2021
Convert javascript object into html table, where keys are in the first column and values are in the second column. This uses plain javascript so you can use it anywhere.
JavaScript

Explode / split string in ES6

15 Jul, 2020
How to split string in ES6. I would suggest the method of using split function from lodash. In modern JavaScript frontend you use a lot of dependencies and lodash is used in almost every project....
JavaScript

Validate Date in JavaScript

15 Apr, 2020
How to find out if a Date is indeed a valid Date. Simply instance check will not do it, the date might be "Invalid Date".
JavaScript

Create and Post HTML Form using Javascript

20 Mar, 2019
This snippet creates html form and submits it, the same way as browser submits form "with redirect". If you need the redirect not an ajax solution, for example credit card payment.
JavaScript

Convert to Integer in JavaScript

8 Nov, 2018
There are multiple ways to convert values to integer in javascript. Using Math.trunc gives the most accurate results.
JavaScript

Webpack: Remove comments from compressed css

1 Jun, 2017
You can minify css with optimize-css-assets-webpack-plugin, but by default some comments are left untouched. If you want to remove all comments you have to use an option for the cssnano, that is used...
JavaScript

Javascript: move item to end of array (of objects), by property value

29 May, 2017
The snippet shows how to move item to the end of array. In a case where we have an array of objects and we want to move the array item by property value. In this example I am using lodash/underscore...
JavaScript

JavaScript ES6: Clone Object or Array

13 May, 2017
Whether you're using vanilla JavaScript or popular frameworks like React, the ability to create deep copies of objects proves invaluable. Take control of your data structures, sidestepping unintended...
JavaScript

webpack: Minify CSS in production

14 Mar, 2017
You can achieve this by adding a library that supports css minify to "webpack.config.js". In this snippet I use "optimize-css-assets-webpack-plugin" which uses cssnano for minimizing by default. The...
JavaScript

Delete object key and value in javascript

12 Oct, 2016
Deleting object value with key in javascript. For this you can use the delete operator. Permits you to remove a property from an object.
JavaScript

Minify JavaScript from command line

8 Mar, 2016
This snippet shows how to minify javascript files from command line. Minifying JavaScript is useful in many respects. Most important are security and performance. Security is increased due to the...
JavaScript

Get previous / next element using JavaScript

8 Mar, 2016
This snippet shows how to get element next to your element / selector. This a part of dom traversal. Using javascript for next element parsing can be used to create some powerful dynamic content. I...
JavaScript

AddThis: Popup instead of hover box

25 Nov, 2015
AddThis is an awesome social sharing tool. It is fully customizable. I suggest using addthis module for loading add this library, this has performance options for the javascript loading.
JavaScript

Get cursor position in CKEditor

12 Jun, 2015
Get cursor position in CKEditor is simple if you know some facts. You have to have the instance initiated, so simple jQuery ready would not work. You can get selection and use that functionality to...
JavaScript

Alter URL GET parameters using JavaScript

14 Aug, 2013
This function alters url get parameters using javascript. The function takes to consideration all aspects of url (parameter existis, hash, parameter doesn't exist). For an example if you need to be...
JavaScript

jQuery - Check if selector is disabled

7 Mar, 2013
You can use "is" to see if the form element is disabled.
JavaScript

Message before leaving the page - JavaScript / jQuery

8 Feb, 2013
How to display message before closing the page or moving to another page. Don't use this to annoy people. Sometimes this functionality is still useful, for an example if you have some sort of unsaved...
JavaScript

Get URL variables with JavaScript

21 Nov, 2012
This function gets URL variables in JavaScript. Current page url parameters will be parsed into array, that will be returned.
JavaScript

Monochrome Google Maps

19 Nov, 2012
This example shows how to create monocrome Google Maps. Where the map will be using only shades of grey (with or without black and/or white). It might also be called grayscale or black-and-white.
JavaScript

Round numbers in JavaScript

1 Oct, 2011
This snippet shows you how to round a number to a specified number of decimal places in JavaScript.
JavaScript

Print JavaScript object

21 Sep, 2011
This snippet shows how to print JavaScript object.
JavaScript

Switching Images With Arrows in JavaScript

7 Apr, 2011
This tutorial shows how to make image switcher with javascript. You can switch images by using arrows.
JavaScript

Loading AJAX Page / Dynamic Content

25 Aug, 2010
Tutorial about loading page with AJAX between any HTML tags. We are using jQuery JavaScript library to do this.