Every once in a while, you come across something and you think, how did I not know this earlier? valueAsNumber
is that thing for me. Whenever we need the value of an HTMLInputElement
, we go to e.target.value
. But getting the value of an input element as a number can be done without parseInt or parsing it.
HTMLInputElement.valueAsNumber
is an attribute that returns a numeric value of the input field and it returns NaN
if the value cannot be converted.
/*
Assuming an <input type="number" value="3.14" />
*/
console.log(input.valueAsNumber) // 3.14
JavaScriptSome things to note:
type="number"
input.valueAsNumber = 45
is valid too.HTMLInputElement.valueAsDate
to get the date from an input with type="date"
input.valueAsNumber = new Date()
I recently switched completely to the Brave browser and have set ad blocking to aggressive…
I was preparing a slide deck for a hackathon and decided to put in a…
I have been using npx a lot lately, especially whenever I want to use a…
Manually copy-pasting the output of a terminal command with a mouse/trackpad feels tedious. It is…
While working on a project, I wanted to do an integrity check of a file…
Popovers have been a problem that was typically solved by using a third-party solution. But…