Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9184359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:01:40+00:00 2026-06-17T19:01:40+00:00

I am trying to convert a HTML5 range input into a group of radio

  • 0

I am trying to convert a HTML5 range input into a group of radio boxes. Each radio box corresponds to a value in the original range. Next to each radio box there is text displaying it’s value.

For some reason, the last bit of text, which should display the value of the last radio box, is not appearing. There should be a 10 on the far right of this image.

enter image description here

Here is a demo.

HTML

<div class='range_container'>
    1<input type="range" name="points" min="1" max="10" value='5'>10
    <div class='range_value_display'>5</div>
</div>

CoffeeScript

($ "input[type='range']").each (i) ->
    initial_value = ($ this).val()
    radio_name = Math.random().toString(36).substr(2, 5)
    start_value = parseInt(($ this).attr('min'))
    end_value = parseInt(($ this).attr('max'))
    for option_number in [start_value..end_value]
        opts = "#{if opts then opts else ''}<input type='radio' name='#{radio_name}' value='#{option_number}'#{if option_number is parseInt(initial_value) then ' checked' else ''}>#{option_number}"
    ($ this).closest('.range_container').replaceWith ($ opts)

JavaScript

($("input[type='range']")).each(function(i) {
    var end_value, initial_value, option_number, opts, radio_name, start_value, _i;
    initial_value = ($(this)).val();
    radio_name = Math.random().toString(36).substr(2, 5);
    start_value = parseInt(($(this)).attr('min'));
    end_value = parseInt(($(this)).attr('max'));
    for (option_number = _i = start_value; start_value <= end_value ? _i <= end_value : _i >= end_value; option_number = start_value <= end_value ? ++_i : --_i) {
        opts = "" + (opts ? opts : '') + "<input type='radio' name='" + radio_name + "' value='" + option_number + "'" + (option_number === parseInt(initial_value) ? ' checked' : '') + ">" + option_number;
    }
    return ($(this)).closest('.range_container').replaceWith($(opts));
});

What is going wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:01:41+00:00Added an answer on June 17, 2026 at 7:01 pm

    The $ opts part is getting confused by the lack an outer element and just dropping the trailing bit of text. Try this out in your JS console:

    $("<span/>5");
    

    And notice there’s no 5 in the resulting jquery object. OTOH:

    $("<div><span/>5</div>");
    

    works fine. So you need to wrap the whole thing in some kind of container.

    Your approach is also a bit confusing in general (progressively replacing the same bit of DOM). I also found it pretty hard to read because so much stuff is crammed into that input-creation line. Here’s what I’d do:

    ($ "input[type='range']").each (i) ->
      $this = $ this
      initial_value = parseInt($this.val())
      max = parseInt($this.attr 'max')
      min = parseInt($this.attr 'min')
      radio_name = Math.random().toString(36).substr(2, 5)
      $opts = $ '<span>'
    
      for option_number in [min..max]        
        input = $('<input>')
          .attr('type', 'radio')
          .attr('name', radio_name)
          .val(option_number)
        $opts
          .append(input)
          .append(option_number)
        input.attr("checked", true) if initial_value is option_number
    
      $this.closest('.range_container').replaceWith $opts
    

    That feels a lot more readable to me. Also makes the “10” show up.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to play with google's swiffy to convert as2 content into html5.
I am trying to convert a HTML string into a .PNG file. Thanks to
I'm trying convert all special chars into HTML safe entities on their way into
I am trying to convert the following c# code into java abstract class BaseProcessor<T>
I have an XML file and I'm trying convert it into a (table( HTML
I am trying to convert my php array into html using php implode. This
I'm trying to convert my sample HTML output into a plain text but I
I am trying to convert HTML into a PDF document in Django and haven't
I'm trying to convert to HTML5, but to preserve the site experience for those
I'm trying to convert the following Java code into Scala: ImageRecognitionPlugin imageRecognition = (ImageRecognitionPlugin)nnet.getPlugin(ImageRecognitionPlugin.class)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.