Halt the jQueryUI Slider once a specific difference or margin has been reached

I have a specific margin or distance that needs to be changed from an HTML select option tag dynamically in the future. I have implemented a Range Slider using jQueryUI Slider.

My objective: I am looking to halt the sliding action once a specific distance, or margin, is reached. Any assistance would be greatly appreciated.

JSFIDDLE: http://jsfiddle.net/3j3Um/1/

HTML

<div id="slider-range"></div>

jQuery

 $("#slider-range").slider({
        range: true,
        min: 0,
        max: 95,
        values: [20, 80],
        slide: function (event, ui) {
           var getMargin = ui.values[0] - ui.values[1];
            $("#margin").val(getMargin);
            if (getMargin == -30) {
                alert('stop the sliding');
            }
        }
    });

Answer №1

In my opinion, you're on the right track. All you need to do is add a return statement when the condition is met. Also, make sure to change the comparison operator to (<) since the slide function is triggered each time a slide action occurs.

I've made some changes to the code to get it functioning (not the cleanest solution but you can refactor it later):

var getMargin = ui.values[0] - ui.values[1];
var rangeValue = (-1) * parseInt(getMargin);
if(rangeValue < 30){
         return false;
}
$("#amount").val("$" + ui.values[0] + " - $" + ui.values[1]);
$("#margin").val(getMargin);

You can also test this out on Working Fiddle

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

I am interested in generating a Stacked Chart in Google Charts using data from a JSON file

Hey there, I'm looking to generate a stacked chart using JSON data in Google Charts. My current challenge revolves around the code snippet var data = google.visualization.arrayToDataTable([. <?php $tmp = array(); require "configdashboard.php"; /* ...

Loading HTML content in Electron using the renderer process JS

During the development phase, I found that the jquery load method for loading HTML views in Electron was functioning smoothly: app.appContainer.load('html/homePage/homePage.html', () => { app.addListenerToHomePage(false) }); I have individua ...

Unexpected form submission behavior

<?php require_once("./include/membersite_config.php"); if(!$fgmembersite->CheckLogin()) { $fgmembersite->RedirectToURL("index.php"); exit; } include 'header.php'; include './include/sql/connect.php'; if(isset($_POST['su ...

Tips for avoiding a ligature occurrence in a specific location

I am a fan of ligatures in general, as they improve readability. I want to implement them across all my HTML pages. However, there is this one word Hanftierheft (which is German and a compound word made up of Hanf, Tier, and Heft). I specifically do not w ...

Utilizing ng-click within a tooltip

I've implemented Angular Bootstrap UI and successfully added a tooltip to my project. Snippet of HTML: <div ng-app="helloApp"> <div ng-controller="helloCtrl as hello"> <a tooltip-trigger="click" tooltip-placement="bottom" uib-t ...

Troubleshooting problem with Vuetify's parallax fluid template

In my quest to implement a fluid template for responsive design viewports, I encountered an issue. The layout looks good on medium viewports (md) but fails to scale correctly on smaller viewports (sm or xs). This is because the height of the parallax backg ...

The padding on this button seems to be arbitrary and nonsensical

I attempted to create a button with a red border that is positioned at the bottom and center, but the padding seems to be interfering with my design. It appears that with the border, the width extends to 100% which doesn't make sense. I have tried va ...

Enhancing the appearance of child elements using CSS modules in Next.js

My Countdown component implementation includes: import styles from "./Countdown.module.scss"; import React from "react"; import useTimer from "hooks/useTimer"; import cn from "classnames"; function Countdown({ date, ...

React Image Slider not loading pictures

Can someone help me troubleshoot why my images are not displaying in the slider on my homepage? I have set up the slider using React Slideshow Image, but the images are not showing up for some reason. I am also wondering if anyone knows of any full-screen ...

Position the div so that it is aligned with the right edge of the

I'm attempting to create a button (div with background color) positioned just below an image that aligns with the right side of the image. Both elements are contained within a wrapper div. When I float the button to the right, it causes the wrapper di ...

Next.js allows you to create a single page that corresponds to the root path '/' as well as a dynamic route '/param'

I have a single-page website built with Next.js. The home page, which displays a list of products, is located at route / and the corresponding code can be found in pages/index.js. Each product has an id, allowing users to jump directly to it using /#produc ...

How can I reference a function in a single file component using Vue.js?

Within my Vue.js project, I have crafted a single file component known as Password.vue which comprises two password fields along with their associated validation checks. To begin with, I structure my HTML within the <template></template> tags, ...

JQuery Function for Repeatedly Looping through Div Elements

I've been experimenting with creating a loop that alternates the fade-in and fade-out effects for different elements. This is what I have so far: setInterval(function() { jQuery(".loop").each(function(index, k) { jQuery(this).delay(1200 ...

Breaking down JavaScript arrays into smaller parts can be referred to

Our dataset consists of around 40,000 entries that failed to synchronize with an external system. The external system requires the data to be in the form of subarrays sorted by ID and created date ascending, taken from the main array itself. Each ID can ha ...

Is it possible for a Javascript code to execute multiple tasks upon being inserted into the browser's URL bar?

Is it feasible for JavaScript to simulate a user clicking on a link, triggering a pop-up, automatically clicking a button within the pop-up, and then redirecting the user to a final page once a specific code is copied and pasted into the browser's add ...

What are some solutions for adjusting this sidebar for mobile devices?

My mobile version sidebar is not functioning properly on my website. To see the issue, you can visit Zinexium. As I am new to HTML, I don't know how to fix it. Here is a link to the code. Thank you! <!DOCTYPE html> // Code snip ...

Using jQuery to reveal the following unordered list after an H3 element by sliding it

<h3>Details<span class="dropdown">View</span></h3> <div id="sbc"> <ul> <li><h2>Role: Supervisor</h2></li> <li>Manager Contact:</li> < ...

Navigating through a table using jQuery to find a specific element

There are form input elements within an HTML table structured like this: <table> <thead> .... </thead> <tr> <td><input type="text" name="n_time" id="5030c9261eca0" value="2012" /></td> ...

Tips for choosing newly added elements using jQuery selectors

Is there a way to select new elements that are dynamically added to the page and have the rel="tooltip" attribute using jQuery? I have tried using the $("[rel='tooltip']").tooltip(); selector, but it doesn't seem to work when new elements ar ...

Automatically simulate the pressing of the enter key in a text field upon page load using Javascript

I am looking to simulate the pressing of the enter key in a text field when a page is loaded. Essentially, I want the text field to automatically trigger the enter key press event as if it had been pressed on the keyboard by the user. Below is an example o ...