Friday, December 20, 2024
HomeWeb DevelopmentEnter valueAsNumber

Enter valueAsNumber


Each every so often I study a JavaScript property that I want I had recognized about years earlier — valueAsNumber is one in all them. The valueAsNumber supplies the worth of an enter[type=number] as a Quantity sort, as an alternative of the normal 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’s going to return NaN if the enter is empty.

Thanks to Steve Sewell for making me conscious of valueAsNumber!

  • Detect DOM Node Insertions with JavaScript and CSS Animations
  • CSS 3D Folding Animation
  • Using MooTools For Opacity

    Though it is attainable to obtain opacity utilizing CSS, the hacks concerned aren’t fairly. Should you’re utilizing the MooTools JavaScript library, opacity is as straightforward as utilizing a component’s “set” methodology. The next MooTools snippet takes each picture with the “opacity” class and units…

  • PHP Woot Checker &#8211; Tech, Wine, and Shirt Woot

    If you have not heard of Woot.com, you have been residing beneath a rock. For many who have been beneath the proverbial rock, this is the plot: Each day, Woot sells one product. As soon as the merchandise is offered out, no extra objects can be found for buy. You do not know what number of…


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments