Having fun with HTML5 – Range type input

Yan Cui

I help clients go faster for less using serverless technologies.

Besides the much talked about video tag, HTML5 also introduced a couple of new input types, one of which is a rather interesting range input type which basically translates to a slider bar control:

image

The range input type is currently supported by the latest versions of Safari, Chrome and Opera while other browsers such as Firefox simply treat the field as a textbox.

There are four available attributes – min, max, step and value. min and max should be self explanatory, step determines the size of each increment/decrement and value is the current value of the slider and also determines the placement of the slider when the element is loaded (if not specified, the slider will appear at the centre of the control when it’s first loaded):

<input type="range" min="0" max="200" step="5"></input>

image

<input type="range" min="0" max="200" value="0" step="5"></input>

image

I talked about the new border radius property being introduced in CSS3 in my previous blog post, and with a little JQuery I’ve put together a quick demo here of how a 400px by 400px square looks as the border radius changes.

If you look at the source of the page you will see that I added a changeBorder javascript function to handle the onchange event fired by the slider, which dynamically updates the HTML content of an internal CSS class called mySliderBarStyles using the current value of the slider control:

<input id="mySliderBar" type="range" min="0" max="200" value="0" step="5"
       onchange="changeBorder(this.value)"></input>
…
<script type="text/javascript">
    function changeBorder(newValue) {
        var newRadius = newValue + "px";
        // set the html content of the label showing the current radius value
        $("#rangeValue").html(newRadius);
        // set the style with the new radius value
        $("#mySliderBarStyles").html(
            "#myDiv { -webkit-border-radius: " + newRadius + "; }"
        );
    }
</script>
…
<style type="text/css" id="mySliderBarStyles"></style>

References:

Dive Into HTML5

Whenever you’re ready, here are 3 ways I can help you:

  1. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
  2. I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
  3. Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.

Leave a Comment

Your email address will not be published. Required fields are marked *