HTML5 Day 6: New Range Input Type <input type=range />

22. November 2010

Some times a Range slider is the appropriate UI tool to allow a user to select a number within a given range. HTML5 adds such an element with it’s new “range” input type.

HTML5_Day6_InputRange_Screenshot

Display in Modern Browsers

In the above screenshot shows an example of the <input type=range/> element as it is rendered in the Opera web browser.

Here’s the code for the range input shown:

<input type=range
    min=0
    max=100
    value=25
    step=5 />

This code defines a range input that allows the user to select value from 0 to 100 with increments of 5.

There are a couple new attributes added to the input element for the new “range” type. The definitions of these attributes are:

  • min – The expected lower bound for the element’s value.
  • max – The expected upper bound for the element’s value.
  • step Specifies the value granularity of the element’s value.

One thing to remember about these attributes is they are really just suggestions. You’ll still want to validate the field on post back, as with any form field.

What about Older Browsers?

Just as with the New Date Input type, the Range Input will be rendered as a standard textbox in older browsers. This will allow users to still use the field to enter a number, but they will need to type in it’s value. This may not be your desired input. To get a nice range slider to show in older browsers you can employ a little help from jQuery and jQuery UI.

The right side of the screenshot at the top of this post shows an example of the jQuery UI Slider being used to augment the <input type=range /> UI in older browsers (Firefox in this case) that do not natively support it.

Here’s some fairly simple jQuery code that will loop through all <input type=range /> elements in the page and essentially convert them to jQuery UI Sliders.

$(function(){
    // Check if browser supports <input type=range/>
    var i = document.createElement("input");
    i.setAttribute("type", "range");
    var rangeNotSupported = (i.type === "text");
    delete i;

    // If browser doesn't support <input type=range/>
    // then use jQuery UI to display them
    if(rangeNotSupported) {
        // loop through all <input type=range/>
        // on the page
        $("input[type=range]").each(function(){
            var range = $(this);
            
            // Create <div/> to hold jQuery UI Slider
            var sliderDiv = $("<div/>");
            sliderDiv.width(range.width());
            
            // Insert jQuery UI Slider where the
            // <input type=range/> is located
            range.after(
                sliderDiv.slider({
                    // Set values that are set declaratively
                    // in the <input type=range/> element
                    min: parseFloat(range.attr("min")),
                    max: parseFloat(range.attr("max")),
                    value: parseFloat(range.val()),
                    step: parseFloat(range.attr("step")),
                    // Update the <input type=range/> when
                    // value of slider changes
                    slide: function(evt, ui) {
                        range.val(ui.value);
                    },
                    change: function(evt, ui) {
                        // set <input type=range/> value
                        range.val(ui.value);
                    }
                })
            ).
            // Hide <input type=range/> from display
            hide();
        });
    }
});

I believe I have commented the above code well enough to make it self explanatory. If you have any questions, feel free to leave a comment or consult the jQuery and /or jQuery UI documentation.

Additional Resources

Conclusion

The new functionality added with HTML5 are all much needed additions to building web applications with HTML. Fortunately, you can add support for those features to older web browsers with a little help from some of the modern JavaScript frameworks, like jQuery. Don’t let older web browsers hold you back from using HTML5.

Oh, BTW, this code works in Internet Explorer, Firefox, Opera, Chrome and Safari!

HTML, JavaScript , , , ,

Comments

12/3/2010 7:24:18 PM #
Good blog, where did you come up with the knowledge in this piece of content? I’m glad I found it though, ill be checking back soon to see what other articles you have.
12/4/2010 11:16:28 AM #
I'll share this article to 2 of my websites. I'll think everyone else will enjoy this Smile.
12/12/2010 5:15:17 AM #
I was looking for some info on product reviews and found your blog. It was a good read and I saved it for future reference
12/12/2010 7:12:13 AM #
thanks for the article yah.
12/12/2010 4:09:44 PM #
Thank you very much for this great article. The best wishes for you Smile
12/12/2010 6:31:22 PM #
I acknowledge to the commenter above amazing blog post btw
12/12/2010 11:28:07 PM #
You certainly deserve a round of applause for your post and more specifically, your blog in general. Very high quality material.
12/13/2010 1:33:06 AM #
Obesity whatsoever is not acceptable. There are many academics who have researched this arena over many years with the statistical answers comprising one of western standards debasement namely food eating habits and corporate avarice over customer wellbeing
12/13/2010 5:08:12 AM #
I am having problem with the first link. It gives a 404 error?  

Regards!


12/13/2010 5:34:43 AM #
Working like a nursing assistant isn't a task as simple as it might look prior to you get into it. You will need to virtually apply all of your information as well as skills learned during the diploma course. In order to perform well virtually, you need to choose a nursing college which can be affiliated with an equally accredited nursing property, hospital as well as any other health facility exactly where you can practice your newly learned skills daily.
12/13/2010 6:29:49 AM #
lol a couple of the reviews bloggers write are silly and unrelated, there are times i wonder whether they at all read the post before writing or whether they just look at the subject of the blog post and compose the very first thought that drifts to their minds. But it is great to see a intelligent commentary every now and then in contrast to the exact same, traditional blog bull which I too many times notice on the blogs. Regards PokemonCraft Admin.
12/13/2010 6:57:58 AM #
This is awesome stuff, its sweet to be in the know.
12/13/2010 7:21:10 AM #
Friend of mine recommend me to visit your website, I found the this what I looked for . Thank you very much
12/13/2010 8:06:21 AM #
A Informative post which was really helpful to me. Thank you.
12/13/2010 8:37:46 AM #
A Informative post which was really helpful to me. Thank you.
12/13/2010 9:19:13 AM #
This is really good news. Thank you for sharing it with us!
12/13/2010 9:51:39 AM #
A Informative post which was really helpful to me. Thank you.
12/13/2010 12:39:51 PM #
Geat post, thansk! I'am sure will be back here soon!
12/13/2010 3:31:21 PM #
When are you going to post again? You really entertain  a lot of people!
12/13/2010 7:28:11 PM #
How do you make a site look this sick! Email me if you can and share your wisdom. .
12/13/2010 9:35:59 PM #
Hi, nice write-up. We're huge fans of nitric oxide products, including Nitrix and SuperPump 250, but it's all about eating right. I've generally been a follower of a Paleolithic regimen. It is natural and how what we're most adapted to follow. Take a look at it when you have a chance and let me know what you think. Anyway... nice blog... I'm subscribed to your feed now so I'll check in more often!
12/13/2010 9:50:33 PM #
While searching I was able to find this Blog and so glad I did.
12/13/2010 10:27:49 PM #
I know you are using Microsofts BlogEngine.NET but can you please tell me which version that you are using?  I've been looking all over and wanted one like the one here at %BLOGSITE% I came from wordpress and am trying to figure out BlogEngine.
12/13/2010 10:29:49 PM #
lots of people believe that recovery is really a burden, but I as a pleasure.
12/13/2010 10:48:10 PM #
That is excellent to know, you gave me some thing to consider on the way house from work. Will you be writing extra about this topic? FYI I tried your rss button and it didnt work. I will try once more in a few hours.
12/14/2010 12:36:34 AM #
I definitely that this content is the most post that I've ever read in my life.
12/14/2010 2:11:17 AM #
your blog is amazing. Amazingly one I had glanced.
12/14/2010 2:14:02 AM #
Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with extra information? It is extremely useful for me.