Quantcast
Channel: CSS-TricksCSS-Tricks
Browsing index pages (31 articles)

The .classList() API

Assuming you have an element in the DOM: <div id="el"></div> Get a reference to that DOM element: const el = document.querySelector("#el"); Then you can manipulate the classes on that...

View Article


Inject HTML From a String of HTML

Say you have some HTML that is a string: let string_of_html = ` <div>Cool</div> `; Maybe it comes from an API or you've constructed it yourself from template literals or something. You can...

View Article


Image may be NSFW.
Clik here to view.

Getting First and Last Items in Array (and splitting all the rest)

I needed to get the first item from an array lately in JavaScript. Hmmmm … lemme think here. I remember that .pop() is for snagging the last item from the array, like this: const arr = ["This",...

View Article

Required Parameters for Functions in JavaScript

Ooo this is clever! I'm snagging this from David's blog. const isRequired = () => { throw new Error('param is required'); }; const hello = (name = isRequired()) => { console.log(`hello ${name}`)...

View Article

Remove Duplicates from an Array

Compiled by Svein Petter Gjøby: const array = [1, 1, 1, 3, 3, 2, 2]; // Method 1: Using a Set const unique = [...new Set(array)]; // Method 2: Array.prototype.reduce const unique =...

View Article


Alphabetizing Arrays, Objects, and Arrays of Objects

An array: let fruits = [`bananas`, `Apples`, `Oranges`]; You can sort of alphabetize that as easy as: fruits.sort(); But notice the inconsistent casing in the array... uppercase characters will all be...

View Article

Add a Number to Two Variables At Once

You can initialize two variables to the same value at once, kinda: var foo, bar; foo = bar = 10; But there is no similarly easy mechanism to add, say, 5 to both foo and bar at the same time. Of course,...

View Article

Replacements for setInterval Using requestAnimationFrame

When it comes to animation, we're told that setInterval is a bad idea. Because, for example, the loop will run regardless of anything else going on, rather than politely yielding like...

View Article


Select Random Item from an Array

var myArray = [ "Apples", "Bananas", "Pears" ]; var randomItem = myArray[Math.floor(Math.random()*myArray.length)]; (more…)

View Article


Convert Polygon to Path Data

I've had to do this a few times recently so I thought I'd save the method. StackOverflow has a method that works great: [].forEach.call(polys,convertPolyToPath); function convertPolyToPath(poly){ var...

View Article

Alphabetizing Arrays, Objects, and Arrays of Objects

An array: let fruits = [`bananas`, `Apples`, `Oranges`]; You can sort of alphabetize that as easy as: fruits.sort(); But notice the inconsistent casing in the array... uppercase characters will all be...

View Article

Add a Number to Two Variables At Once

You can initialize two variables to the same value at once, kinda: var foo, bar; foo = bar = 10; But there is no similarly easy mechanism to add, say, 5 to both foo and bar at the same time. Of course,...

View Article

Replacements for setInterval Using requestAnimationFrame

When it comes to animation, we're told that setInterval is a bad idea. Because, for example, the loop will run regardless of anything else going on, rather than politely yielding like...

View Article


Select Random Item from an Array

var myArray = [ "Apples", "Bananas", "Pears" ]; var randomItem = myArray[Math.floor(Math.random()*myArray.length)]; (more…)

View Article

Convert Polygon to Path Data

I've had to do this a few times recently so I thought I'd save the method. StackOverflow has a method that works great: [].forEach.call(polys,convertPolyToPath); function convertPolyToPath(poly){ var...

View Article


Cross-Browser Dependency-Free DOM Ready

Denis Ciccale's version: var DOMReady = function(a, b, c) { b = document c = 'addEventListener' b[c] ? b[c]('DocumentContentLoaded', a) : window.attachEvent('onload', a) } DOMReady(function () {...

View Article

Remove an Element

For whatever reason, an element can't destroy itself in JavaScript. jQuery has a method for this, which is nice because this is how we think: $(".remove-me").remove(); But there is no direct equivalent...

View Article


Loop Over querySelectorAll Matches

You can loop over Arrays really easily in JavaScript with forEach, but unfortunately it's not that simple with the results of a querySelectorAll. /* Will Not Work */...

View Article

Print Object To Screen

PHP has a nice print_r function for printing out information about a variable to the screen. console.log() is great for that in JavaScript also, but sometimes you just need/want to look at it on the...

View Article

Check if font-family is Honored

function checkFont(strFamily) { var objDiv = document.createElement('div'); objDiv.style.fontFamily = strFamily; objDiv.appendChild(document.createTextNode('FONT TEST')); if (window.getComputedStyle) {...

View Article
Browsing index pages (31 articles)


Latest Images