How to create Form Element Themed Range slider using jQuery Mobile?


Developing a Form element Themed Range slider with jQuery Mobile is a straightforward yet effective approach to uplift user experience on webpages. This attribute empowers users to pick a value within an assigned range through the smooth movement of a handle along a customized track. By adding themed range sliders, website designers can provide instinctive input choices that augment the convenience and aesthetics of their forms.

Method 1: Using Default jQuery Mobile Range Slider

The simplest method is to use the default jQuery Mobile range slider. It provides a basic, pre−styled range slider that can be customized to match your theme.

Algorithm

Step 1: Import necessary components: jQuery Mobile library and CSS files.

Step 2: Create an input element and assign a unique identifier.

Step 3: Give the input element a slider identity.

Step 4: Customize the slider's appearance by adding specific themes

Step 5:Define the slider's range.

Step 6: Set the initial value.

Example

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
  <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
  <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
  <div data-role="page">
    <div data-role="header">
      <h1>Themed Range Slider</h1>
    </div>
    <div data-role="main" class="ui-content">
      <orm>
        <label for="slider-1">Slider:</label>
        <input type="range" name="slider-1" id="slider-1" data-track-theme="a" data-theme="b" min="0" max="100" value="50">
      </form>
    </div>
  </div>
</body>
</html>

Method 2: Customizing the Default Range Slider

The second method involves customizing the default jQuery Mobile range slider to achieve a unique appearance.

Algorithm

Step 1: Follow steps 1−5 from Method 1.

Step 2: Add a <style> section within the <head> of your HTML document.

Step 3: Customize the appearance of the range slider

Step 4: Specify the desired CSS properties, such as background−color, border, height, margin, etc.

Step 5: Optionally, modify the appearance of the range slider handle using the .uis−lider−handle selector.

Step 6: Test and refine the customized range slider by adjusting the CSS properties until you achieve the desired visual style.

Example

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
  <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
  <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
  <style>
    /* Custom styles for the range slider */
    #slider-2 {
      background-color: black;
      border: 1px solid #ddd;
      height: 10px;
      margin: 15px 0;
    }
    #slider-2 .ui-slider-bg {
      background-color: #007bff;
    }
    #slider-2 .ui-slider-handle {
      background-color: #007bff;
      border-color: #007bff;
    }
  </style>
</head>
<body>
  <div data-role="page">
    <div data-role="header">
      <h1>Customized Range Slider</h1>
    </div>
    <div data-role="main" class="ui-content">
      <form>
        <label for="slider-2">Slider:</label>
        <input type="range" name="slider-2" id="slider-2" data-track-theme="a" data-theme="b" min="0" max="100" value="50">
      </form>
    </div>
  </div>
</body>
</html>

Method 3: Using External Range Slider Libraries

The third method involves utilizing external range slider libraries with more advanced features and customization options.

Algorithm

Step 1: "Add necessary jQuery Mobile library and CSS files to HTML document.

Step 2: Choose an external range slider library like Ion.RangeSlider or noUiSlider.

Step 3: Include the library's CSS and JavaScript files in HTML, specifying their URLs.

Step 4: Create an input element with a unique ID and required attributes.(Step 2 from method 1)

Step 5: Use JavaScript to initialize the range slider with proper configuration options (Follow Steps 3 to 5 from method 1).

Step 6: Customize the design using library APIs and jQuery−based scripts for personalized needs.

Example

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
  <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
  <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
  
  <!-- Include the external range slider library -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ion-rangeslider/2.3.1/css/ion.rangeSlider.min.css" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/ion-rangeslider/2.3.1/js/ion.rangeSlider.min.js"></script>
</head>
<body>
  <div data-role="page">
    <div data-role="header">
      <h1>External Range Slider</h1>
    </div>
    <div data-role="main" class="ui-content">
      <!-- Form for the range slider -->
      <form>
        <label for="slider-3">Choose a value:</label>
        <input type="text" name="slider-3" id="slider-3">
      </form>
    </div>
  </div>
  
  <script>
    // Function to initialize the range slider
    function initializeRangeSlider() {
      var slider = $("#slider-3");
      var minValue = 0;
      var maxValue = 100;
      var defaultValue = 50;

      // Set up the options for the range slider
      var options = {
        min: minValue,
        max: maxValue,
        from: defaultValue,
        // Additional customization options can be added here
        // ...
      };

      // Initialize the external range slider
      slider.ionRangeSlider(options);
    }

    // When the page is created, call the function to initialize the range slider
    $(document).on("pagecreate", function() {
      initializeRangeSlider();
    });
  </script>
</body>
</html>

Conclusion

In conclusion, the best method for creating a themed range slider can depend on your specific project requirements. Those who need simple and sorted can opt for Method 1 as it provides effortless and pre−styled options. Alternatively, if you are searching for greater control over both function and appearance than considering using Method 2 as it grants users more flexibility in customizing their sliders' looks.

Finally if looking to elevate your sliders' functionality even further through third party libraries look no further than Method 3 instead for added features like libraries such as ion.rangeslider or noUiSlider. Choose whichever path aligns most closely with your preferred levels of customization plus overall design criteria.

Updated on: 09-Aug-2023

89 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements