Each now and again I find out about a JavaScript property that I want I had recognized about years earlier — valueAsNumber
is considered one of them. The valueAsNumber
offers the worth of an enter[type=number]
as a Quantity kind, as an alternative of the normal string illustration once you get the worth:
/* Assuming an <enter kind="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
Fb Open Graph META Tags
It is no secret that Fb has turn out to be a significant site visitors driver for all sorts of internet sites. These days even giant companies steer shoppers towards their Fb pages as an alternative of the company web sites immediately. And naturally there are Fb “Like” and “Suggest” widgets on each web site. One…
Elegant Overflow with CSS Ellipsis
Overflow with textual content is at all times an enormous problem, particularly in a programmatic setting. There’s at all times solely a lot area however variable content material so as to add into that area. I used to be lately engaged on a desk for displaying person info and observed that longer strings have been…
HTML5’s placeholder Attribute
HTML5 has launched many options to the browser; some HTML-based, some within the type of JavaScript APIs, however all of them helpful. Certainly one of my favorites if the introduction of the
placeholder
attribute to INPUT components. Theplaceholder
attribute exhibits textual content in a area till the…
Source_link