Gets the Unix timestamp from a Date object. Use Date.prototype.getTime() to get the timestamp in milliseconds and divide by 1000 to get the timestamp in seconds. Use Math.floor() to appropriately round the resulting timestamp to an integer. Omit the argument, date, to use the current date. Source https://www.30secondsofcode.org/js/s/get-timestamp
Preventing paste into an input field
See the codepen: https://codepen.io/JSsnippets/pen/qBbyMoJ Source https://github.com/roeib/JavaScript-snippets#preventing-paste-into-an-input-field
Get and set the text of an element (without markup)
Get and set the text of an element without its markup. Source https://vanillajstoolkit.com/reference/html/textcontent/
Range generator
Creates a generator, that generates all values in the given range using the given step. Use a while loop to iterate from start to end, using yield to return each value and then incrementing by step. Omit the third argument, step, to use a default value of 1. Source https://www.30secondsofcode.org/js/s/range-generator
Notify when element size is changed
Source https://github.com/roeib/JavaScript-snippets#notify-when-element-size-is-changed
Get and set HTML content for an element
Note: using innerHTML with third-party or user-submitted content can expose you to cross-site scripting (XSS) attacks. Source https://vanillajstoolkit.com/reference/html/innerhtml/
Check if a node is in the viewport
Source https://github.com/roeib/JavaScript-snippets#check-if-a-node-is-in-the-viewport
Remove an item from an object
Use the delete operator on the key to remove. Source https://vanillajstoolkit.com/reference/objects/delete/
Return an array of elements that appear in both arrays
Use Array.prototype.includes() to determine values that are not part of values. Use Array.prototype.filter() to remove them. Source https://www.30secondsofcode.org/js/s/similarity
Check if a string is a valid JSON
This is useful for making sure that the JSON is valid before you perform another operation on it. You can learn more about how to use json parse on WebReference. While working, this json formatter and validator is a handy tool. Source https://github.com/roeib/JavaScript-snippets#check-if-a-string-is-a-valid-json