Each every now and then I study a JavaScript property that I want I had identified about years earlier — valueAsNumber
is one among them. The valueAsNumber
supplies the worth of an enter[type=number]
as a Quantity sort, as a substitute of the standard string illustration whenever you get the worth:
/* Assuming an <enter sort="quantity" worth="1.234" /> */ // BAD: Get the worth and convert the quantity enter.worth // "1.234" const numberValue = parseFloat(enter.worth, 10); // GOOD: Use valueAsNumber enter.valueAsNumber // 1.234
This property permits us to keep away from parseInt
/parseFloat
, however one gotcha with valueAsNumber
is that it’ll return NaN
if the enter
is empty.
Thanks to Steve Sewell for making me conscious of valueAsNumber
!
PSA: quantity inputs have a `.valueAsNumber` property that you could be discover useful pic.twitter.com/1QwdAW16CC
— Steve Sewell (@Steve8708) March 31, 2022
Web page Visibility API
One occasion that is all the time been missing inside the doc is a sign for when the consumer is taking a look at a given tab, or one other tab. When does the consumer swap off our web site to have a look at one thing else? When do they arrive again?
Create a CSS Dice
CSS cubes actually showcase what CSS has grow to be through the years, evolving from easy colour and dimension directives to a language able to creating deep, artistic visuals. Add animation and you have got one thing actually neat. Sadly every CSS dice tutorial I’ve learn is a bit…
Chris Coyier’s Favourite CodePen Demos
David requested me if I would be up for a visitor publish choosing out a few of my favourite Pens from CodePen. A frightening activity! There are such a lot of! I managed to choose a number of although which have blown me away over the previous few months. When you…
Assign Anchor IDs Utilizing MooTools 1.2
One among my favourite makes use of of the MooTools JavaScript library is the SmoothScroll plugin. I apply it to my web site, my employer’s web site, and on many buyer web sites. The very best half concerning the plugin is that it is really easy to implement. I lately ran…