Is there a way to transform a form-check (radio button) into a 5-point slider on an HTML/JS webpage?

I have developed a survey page and I would like to transform the survey question from a radio button to a 5-point slider. I attempted to utilize the following code snippet:

<input id="ex1" data-slider-id='ex1Slider' type="text" data-slider-min="0" data-slider-max="5" data-slider-step="1"/>

However, it does not seem to be working as expected. Additionally, I need to associate each value with corresponding text similar to how it is structured in my radio button code below. Can someone provide guidance on this and offer a solution? Moreover, I am seeking a way to retrieve the values from these sliders just like I did in the code snippet provided.

HTML :-

<!DOCTYPE html>
<html lang="en">

<head>
  <title>Survey Question</title>
  <meta charset="utf-8">
  
   // Additional HTML content...

</head>

// Additional HTML content...

</body>

</html>

JS :-

function displayRadioValue() {
  let section1 = document.querySelectorAll('.section-1 > input[type="radio"]')
  let section2 = document.querySelectorAll('.section-2 > input[type="radio"]')
  
   // Remaining JavaScript code...

}

CSS:-

@media print {
  body * {
    visibility: hidden;
  }
  .final-results * {
    visibility: visible;
  }
  .final-results {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
  }
}

table,
table tr th,
table tr td {
  border: 1px solid black;
}

Answer №1

bootstrap-slider.js is a helpful tool for enhancing your website functionality.

If you need guidance, check out the bootstrap-slider.js documentation.

You can conveniently access it via CDN with the following code:

<link rel="stylesheet" 
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/11.0.2/bootstrap- 
slider.js" crossorigin="anonymous"></script>

To integrate it into your HTML, use the following snippet:

<input id="ex6" type="text" data-slider-min="0" data-slider-max="5" data-slider- 
step="1" data-slider-value="0" /> <span id="ex6CurrentSliderValLabel">Current Slider 
Value: <span id="ex6SliderVal">3</span></span>

For jQuery implementation, use the below code:

$("#ex6").slider();
$("#ex6").on("slide", function (slideEvt) {
$("#ex6SliderVal").text(slideEvt.value); });

If you want to see a sample snippet of using radio buttons in JavaScript, here's a sample code:

// Your JavaScript code goes here
// Remember to replace this placeholder text with your actual code
@media print {
  body * {
    visibility: hidden;
  }
  .final-results * {
    visibility: visible;
  }
  .final-results {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
  }
}

table,
table tr th,
table tr td {
  border: 1px solid black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/11.0.2/css/bootstrap-slider.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/11.0.2/bootstrap-slider.js"></script>
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Survey Question</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">


  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

</head>

<body>
  <style>
    @media print {
      body * {
        visibility: hidden;
      }
      .final-results * {
        visibility: visible;
      }
      .final-results {
        position: absolute;
        left: 0;
        top: 0;
        right: 0;
        bottom: 0;
      }
    }
    
    table,
    table tr th,
    table tr td {
      border: 1px solid black;
    }
  </style>
  <section class="container py-4">
    <div class="row">
      <div class="col-md-12">
        <h2>Survey</h2>
        <ul id="tabs" class="nav nav-tabs">
          <li class="nav-item"><a href="" data-target="#section1" data-toggle="tab" class="nav-link small text-uppercase active">Section 1</a></li>
          <li class="nav-item"><a href="" data-target="#section2" data-toggle="tab" class="nav-link small text-uppercase">Section 2</a></li>
          <li class="nav-item"><a href="" data-target="#results" data-toggle="tab" class="nav-link small text-uppercase">Results</a></li>
        </ul>
        <br>
        <div id="tabsContent" class="tab-content">
          <div id="section1" class="tab-pane fade active show">
            // Section 1 content goes here
          </div>
              
          <div id="section2" class="tab-pane fade">
            // Section 2 content goes here
          </div>
                        
          <div id="results" class="tab-pane fade">
            // Results content and button go here
          </div>
        </div>
      </div>
    </div>
  </section>
</body>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Conceal an element from view upon clicking on it

Task at Hand : Implement a smooth element hiding effect upon clicking, similar to the behavior on this website , where the letter A fades away smoothly when clicked. Is it possible to achieve this effect using only HTML, CSS, and JavaScript? var t ...

Styling a multilevel list with CSS that includes a combination of

Hello everyone! I am looking to create a mixed multilevel list in CSS, similar to the one shown in this image: image description here. I have attempted the following code snippet, but it seems to apply the counter to unordered lists: ol { counter-res ...

Using NextJS to pass a string from an input field to getStaticProps via context

I am a beginner in NextJS and React, so please forgive my lack of knowledge. I am trying to figure out how to pass the text input by users from an input field (inside Header) to the getStaticProps function of a specific page using the React context API. I ...

Developing a node.js application with express that effectively manages synchronous function calls and value passing in a modularized structure

I am currently developing a node.js/express application to validate user account details in order to determine their eligibility to access a specific resource. Initially, all the code was housed in a single app.js file, but now I am restructuring it into m ...

AJAX jQuery requests can flatten arrays when they are sent

The below code shows an endpoint written in Express, using the body-parser middleware. app.post("/api/poll/new",api.NewPoll); api.NewPoll = function(req,res){ if(!req.body) return res.status(400).send("MISSING BODY"); console.log(req.body,typeof(r ...

I am attempting to retrieve the information entered into the textbox, search for it within my database, and display the results beneath the textbox for reference

<!----- fetchCedulaData.php This script retrieves data from the database and performs a search to return results ---------------------------- - --> <?php require("connection.php"); $cedula=$_REQUEST["cedula"]; //$cedula="0922615646"; echo $cedu ...

html interactive/expandable tree

I've come across this code which generates an HTML tree, but I'm facing an issue where the tree expands every time I refresh the page. What I want to achieve is to have certain branches expanded and others collapsed when the page is opened, depe ...

I'm sorry, there seems to be an issue with connecting to the database at the moment. The error code SequelizeConnectionRefusedError is indicating that the connection is being refused at 127.0

I'm currently facing an issue with my code. I've set up a local MySQL database using XAMPP, but for some reason, sequelize is unable to connect to it. import { Sequelize } from "sequelize"; const user_auth_db = new Sequelize('db_n ...

Interactive dropdown menus using HTML and jQuery

My goal is to dynamically update a group of select boxes when another select box is changed. The challenge I'm facing is getting some of the select boxes to have the same options while allowing for the user to add more selects as needed. There will b ...

Determining if there is a common element in two arrays to yield a true outcome

In my Ionic app built with Angular 1.x, I am dealing with two arrays containing numbers: var arr1 = [1,32,423,43,23,64,232,5,67,54]; var arr2 = [11,32,1423,143,123,64,2232,35,467,594]; The common numbers in these arrays are 32 and 64. I need a JavaScript ...

What is the best way to manage asynchronous functions when using Axios in Vue.js?

When I refactor code, I like to split it into three separate files for better organization. In Users.vue, I have a method called getUsers that looks like this: getUsers() { this.isLoading = true this.$store .dispatch('auth/getVal ...

Acquiring a website's dynamic value using jquery

This question is pretty self-explanatory... I am trying to extract a value from a website's source code, but the value I need is dynamically generated using jQuery. Let's use example.com as an example: <div id="currentTime"></div> ...

Ajax was intended to introduce a session variable, however, it is not functioning as expected

My experience with AJAX is limited, but I am attempting to implement a seemingly simple functionality. I have a dropdown select form field that triggers a JavaScript function containing an AJAX call. <select onchange="update_session_value('123&ap ...

Incorporate a PHP script into JavaScript using AJAX or alternative methods

I have been trying to complete a page on my website for the past couple of hours with the following objectives in mind. When a button is clicked, the following actions should occur: A download link should appear (done - working) The mySQL table should be ...

Creating a dynamic dashed line animation using SVG

Imagine a line growing in the same pattern as this fiddle. svg #path { stroke-dasharray: 19; stroke-dashoffset: 19; animation: dash 4s linear 0s forwards; } @keyframes dash { from { stroke-dashoffset: 19; } to { ...

Validation of forms - Must include one particular word from a given set

I am in the process of utilizing Javascript to validate an input field with the specific formatting requirements outlined below: "WORD1,WORD2" The input must contain a comma separating two words, without any spaces. The first word (WORD1) can be any word ...

An efficient way to create a JavaScript object and dynamically update one of its properties based on the result of another operation

Currently, I am extracting data from Eventbrite's API and creating objects based on the event name and any image tags retrieved from the entire response data. for (var i = 1, l = rawEventbriteData.length; i < l; i++){ var eventObject = { ...

Row in Bootstrap 3 fails to display correctly on medium-sized devices

Having trouble with divs of different sizes? I want a row that shows 3-column wide divs on medium & large screens and 6-column wide divs on small devices. <div class="container"> <div class="row"> <div class="service col-sm-6 col-md-3"& ...

When a JavaScript variable refuses to cooperate and won't populate

I am currently working with a simple variable that is accepting a JSON Object. Here is the code snippet: To maintain privacy, I have sanitized the code to avoid revealing sensitive information. var server = "00.00.000.00:8800"; var webServices = "http:/ ...

Utilizing the CSS property "overflow:hidden;" to control the content visibility within nested div elements

Within a parent div, I have 6 nested divs with no border-radius set. The parent div has a border-radius applied, causing the corners of the child divs to spill over the rounded corners of the parent. Even setting overflow to hidden does not seem to solve t ...