Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JQuery Scroll Limit to Span Text

Firstly please excuse my knowledge of JQuery im a total noob and struggline to get this code to work.
I did try a search but could not find a solution for my issue.

Ok so I have a little scroll bar which allows the user to select a tempo from 40 to 300 bpm
these are the limits I have set for the slider.

Under the slider is a text input box so people can just enter the bpm manually example: 121
If the number entered is lower or highr then the limites meantioned above, I need to set the Span text to the lower or higher limit (example 40 lower limit and 300 max limit)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I also need 2 outputs updating at the same time: one is the text input field and the other is a span

here is the code I have

$('#BPMRange')
  .on('input', function() {
    var value = $(this).val();
    $('#Test').text(value);
  })


$('#BPM')
  .keyup(function() {
    var value = $(this).val();
    $('#Test').text(value);
  })
  .keyup();


$('#BPM').on('change blur', function() {
  if ($(this).val().trim().length === 0) {
    $(this).val($default_BPM);
    var value = $(this).val();
  }

  if ($(this).val() < 40) {
    $('#Test').text('40');
  } else if ($(this).val() > 300) {
    $('#Test').text('300');
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="BPM_Slide_Container">
  <input type="range" min="40" max="300" value="300" class="BPM_Slider" id="BPMRange">
  <br>
  <input name="BPM" type="text" id="BPM" value="300" class="TempoField">
</div>



<span id="Test"></span>

Thank you for any advice you can share – I do appreciate it

>Solution :

Just change the others’ text() or val() on change event.

var range = $('#BPMRange')
var input = $('#BPM')
var test = $("#Test");

range
  .on('input', function() {
    var value = $(this).val();
    test.text(value);
    input.val(value)
  })


input.on('change', function() {
  var value = $(this).val();
  value = Math.min(300, value);
  value = Math.max(40, value);
  range.val(value);
  test.text(value);
  input.val(value)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="BPM_Slide_Container">
  <input type="range" min="40" max="300" value="300" class="BPM_Slider" id="BPMRange">
  <br>
  <input name="BPM" type="text" id="BPM" value="300" class="TempoField">
</div>

<span id="Test"></span>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading