Every once in a while I learn about a JavaScript property that I wish I had known about years earlier — valueAsNumber
is one of them. The valueAsNumber
provides the value of an input[type=number]
as a Number type, instead of the traditional string representation when you get the value:
/* Assuming an <input type="number" value="1.234" /> */ // BAD: Get the value and convert the number input.value // "1.234" const numberValue = parseFloat(input.value, 10); // GOOD: Use valueAsNumber input.valueAsNumber // 1.234
This property allows us to avoid parseInt
/parseFloat
, but one gotcha with valueAsNumber
is that it will return NaN
if the input
is empty.
Thank you to Steve Sewell for making me aware of valueAsNumber
!
PSA: number inputs have a `.valueAsNumber` property that you may find handy pic.twitter.com/1QwdAW16CC
— Steve Sewell (@Steve8708) March 31, 2022
An Interview with Eric Meyer
Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it? At first blush, it was the simplicity of it as compared to the table-and-spacer…
CSS Gradients
With CSS border-radius, I showed you how CSS can bridge the gap between design and development by adding rounded corners to elements. CSS gradients are another step in that direction. Now that CSS gradients are supported in Internet Explorer 8+, Firefox, Safari, and Chrome…
Introducing MooTools ScrollSide
This post is a proof of concept post — the functionality is yet to be perfected. Picture this: you’ve found yourself on a website that uses horizontal scrolling instead of vertical scrolling. It’s an artistic site so you accept that the site scrolls left to right.
Source link