Saturday, June 22, 2024
HomeJavaScriptEnter valueAsNumber

Enter valueAsNumber

Each infrequently I find out about a JavaScript property that I want I had identified about years earlier — valueAsNumber is one in all them. The valueAsNumber offers the worth of an enter[type=number] as a Quantity sort, as a substitute of the standard string illustration once 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!

  • Creating Scrolling Parallax Effects with CSS

    Introduction For fairly a very long time now web sites with the so referred to as “parallax” impact have been actually in style. In case you haven’t heard of this impact, it principally consists of totally different layers of pictures which are shifting in numerous instructions or with totally different pace. This results in a…

  • From Webcam to Animated GIF: the Secret Behind!
  • Create a Simple Slideshow Using MooTools, Part III: Creating a Class
  • jQuery Countdown Plugin

    You have most likely been to websites like RapidShare and MegaUpload that permit you to obtain information however make you wait a specified variety of seconds earlier than providing you with the obtain hyperlink. I’ve created an identical script however my script means that you can animate the CSS font-size…



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments