Increase value by 1 with the push of a button within two specified ranges using JavaScript

I am looking to create buttons that will increase the value of both the first and second range by 1 when pressed, as well as decrease the value of both ranges by 1 when pressed.

Here is my code:

function run(){
  var one = document.getElementById("range1").value;
  document.getElementById('out1').value=one; 
   var two = document.getElementById("range2").value;
  document.getElementById('out2').value=two; 
  }
<input type="range" name="range1" step="1" value="20" min="0" max="100" oninput="run(this.value)"  id="range1"/>
 <output id="out1">20</output>

<input type="range" name="range2" step="1" value="37" min="0" max="100" oninput="run(this.value)"  id="range2"/>
<output id="out2">37</output>
<br>
<button>+</button><button>-</button><br><br><br>
<h2>+ :increment 1 value on both of them</h2>
<h2>- :decrement 1 value on both of them</h2>
and live change

Answer №1

function operate(){
   var valueOne = document.getElementById("range1").value;
   document.getElementById('out1').value=valueOne; 
   var valueTwo = document.getElementById("range2").value;
   document.getElementById('out2').value=valueTwo; 
  }
function addOne(){
   document.getElementById("range1").value++;
   document.getElementById("range2").value++;
   operate();
}
function subtractOne(){
   document.getElementById("range1").value--;
   document.getElementById("range2").value--;
   operate();
}
<input type="range" name="range1" step="1" value="20" min="0" max="100" oninput="operate(this.value)"  id="range1"/>
 <output id="out1">20</output>

<input type="range" name="range2" step="1" value="37" min="0" max="100" oninput="operate(this.value)"  id="range2"/>
<output id="out2">37</output>
<br>
<button onclick="addOne()">+</button><button onclick="subtractOne()">-</button><br><br><br>
<h2>+ :increment 1 value on both of them</h2>
<h2>- :decrement 1 value on both of them</h2>
and live change

  • Defined two functions(and assigned them to the onclick events of the buttons)
  • Both functions add 1 or subtract 1 (++ and -- are shortcuts for +=1 and -=1)
  • Both functions invoke operate() to update the values of output tags.

For more details:

++:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators#Addition_assignment

--:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators#Subtraction_assignment

Answer №2

Below is the code snippet that adds click events to buttons and increments or decrements values accordingly:

function run() {
  var one = document.getElementById("range1").value;
  document.getElementById('out1').value = one;
  var two = document.getElementById("range2").value;
  document.getElementById('out2').value = two;
}

function setNewValue(val) {
  var r1 = document.getElementById("range1");
  r1val = parseInt(r1.value) + val;
  if (r1val >= 0 && r1val <= 100) {
    r1.value = r1val;
    document.getElementById('out1').value = r1val;
  }

  var r2 = document.getElementById("range2");
  r2val = parseInt(r2.value) + val;
  if (r2val >= 0 && r2val <= 100) {
    r2.value = r2val;
    document.getElementById('out2').value = r2val;
  }
}
<input type="range" name="range1" step="1" value="20" min="0" max="100" oninput="run(this.value)" id="range1" />
<output id="out1">20</output>

<input type="range" name="range2" step="1" value="37" min="0" max="100" oninput="run(this.value)" id="range2" />
<output id="out2">37</output>
<br>
<button onclick="setNewValue(1)">+</button>
<button onclick="setNewValue(-1)">-</button>
<br>
<br>
<br>
<h2>+ :increase by 1 for both fields</h2>
<h2>- :decrease by 1 for both fields</h2>
and see live changes

Answer №3

If you're looking for an easy jQuery solution, here's a simple one:

$('#more').click(function() {
  $('#range1').val(parseInt($('#range1').val())+1);
  $('#out1').text($('#range1').val());
  $('#range2').val(parseInt($('#range2').val())+1);
  $('#out2').text($('#range2').val());
});

$('#less').click(function() {
  $('#range1').val(parseInt($('#range1').val())-1);
  $('#out1').text($('#range1').val())
  $('#range2').val(parseInt($('#range2').val())-1);
  $('#out2').text($('#range2').val())
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="range" name="range1" step="1" value="20" min="0" max="100" oninput="run(this.value)" id="range1" />
<output id="out1">20</output>

<input type="range" name="range2" step="1" value="37" min="0" max="100" oninput="run(this.value)" id="range2" />
<output id="out2">37</output>
<br>
<button id="more">+</button>
<button id="less">-</button>
<br>
<h2>+ :increment 1 value on both of them</h2>
<h2>- :decrement 1 value on both of them</h2>
and live change

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

If every object within the array contains a value in the specified property, then return true

In my current project, I am working with an array of objects that looks something like this: $scope.objectArray = [ {Title: 'object1', Description: 'lorem', Value: 57}, {Title: 'object2', Description: 'ipsum', V ...

Encountering a Null Pointer Exception when trying to locate an element on an AngularJS website using Selenium

Testing an AngularJS website with Selenium can be challenging due to angular directives, as mentioned in a blog. Despite encountering some stability issues such as failures and exceptions like "unable to locate Element," "No such element," or "Null pointer ...

Iterating through JSON objects in JavaScript using 'Foreach'

After receiving a JSON Object from my PHP script, I am trying to extract specific data using JavaScript. { "Data":{ "Recipes":{ "Recipe_7":{ "ID":"7", "TITLE":"Wu ...

FilterTextBoxExtender in AJAX enables the input of carriage returns

I need assistance with a multi-line text box that I'm using an AJAX FilteredTextBoxExtender on to restrict user input to just numbers. However, I also want users to be able to add a new line by pressing the enter key. I've looked around for a sol ...

What is the best way to transform a string into emojis using TypeScript or JavaScript?

Looking to convert emoji from string in typescript to display emoji in html. Here is a snippet of the Typescript file: export class Example { emoji:any; function(){ this.emoji = ":joy:" } } In an HTML file, I would like it to dis ...

How does the object scope receive, process, and prepare the update for display within the AngularJs view?

AngularJS allows for data-binding to easily display immediate data in our View. This is made possible by the object scope, which acts as a connector between the Logic Code and The View. Additionally, AngularJs supports two-way binding. My question is: H ...

Using JavaScript to search for a specific string within a row and removing that row if the string is detected

I need help with a script that removes table rows containing the keyword STRING in a cell. However, my current script is deleting every other row when the keyword is found. I suspect this has to do with the way the rows are renumbered after deletion. How c ...

Infinite scrolling with a dynamic background

Hi there, I am working on my website and trying to create a smooth transition between sections similar to the one demonstrated here:. The challenge I'm facing is that the backgrounds of my sections cannot be fixed; they need to have background-attachm ...

Elm div contenteditable loses cursor position after content update

Is there a way to maintain the cursor position while typing in a contenteditable div? I'm utilizing Elm to generate a text box and require the text within it to undergo processing with every input. The rationale behind opting for a contenteditable di ...

jQuery Ajax comes with a built-in method for validating serialized forms. This method allows

After creating a form and serializing it to send to my MVC Action method, here is how my jQuery Ajax script looks: $('#submit').click(function () { var jsonObj = $('#form').serialize(); alert(jsonObj); $.ajax({ ty ...

Dynamically shift the table footer to the bottom of a scrolling div by utilizing jQuery

I'm facing a challenge where I need to relocate each th tag from the footer row of a table to the bottom of a scrolling div. You can check out the scenario on this link. Currently, I am able to achieve this by hardcoding it like so: $('.sticky- ...

Flex Display with Bootstrap for Responsive Tables

Is there a way to display the scrolling of a Bootstrap table using a flex container? https://getbootstrap.com/docs/4.4/content/tables/#responsive-tables I am looking for the JSFiddle example with DIV elements. Removing the display:flex from .flex_containe ...

Is it possible for Javascript, AJAX, or JQuery to determine if a form has been manually inputted into

My goal is to change the contents of a form field and submit the form through the console. However, when I use either jQuery or JavaScript to modify the field and submit it, the page only recognizes values that were manually typed in. In the code snippet ...

What is the ideal event to trigger a response when the user completes entering text into the search field in Vue.js?

I am currently utilizing a text field from the Vuetify library to filter a table within my application. <v-text-field style="min-width: 300px;" v-model="filterString" label="Search" /> The functionality is straigh ...

Tips for accessing the parent method within a jQuery AJAX success function

Similar Question: javascript how to reference parent element Hello everyone! This is my first time posting here. I have a question - how can I trigger alerts from a successful AJAX call? var page = { alerts: function (json) { if (json ...

What is the best way to incorporate web components into a React Js project?

I am currently unfamiliar with web components. My main goal is to integrate Google Material Components into my React.js project. Google Material Web Component can be found here: https://github.com/material-components/material-components-web Is there a ...

The font-size transition using CSS3 is experiencing a lack of smoothness specifically in the

I have crafted a simple animation for an h1 element, utilizing css3 and a transition on the font-size. Here is the link to view: http://jsbin.com/oPIQoyoT/1/edit h1 { position: relative; width:500px; height: 500px; font-size: 3em; tra ...

Rotate images using a JQuery plugin without using HTML tags, solely through passing the images in JavaScript

I am currently working on creating a jQuery image rotator from scratch. While I know there are simple jQuery sliders/rotators available, I specifically want to include the image files directly within the JS code. I do not want to use this HTML structure: ...

The AJAX function fails to trigger the MVC controller method

I'm attempting to enable inline editing by cell, rather than by row, when double-clicking. Although the initial setup is working, it's not updating the record as expected - the "SaveCustomer" call to the controller isn't triggered. Can anyon ...

Build a Docker container for a project that requires utilizing yarn link for dependencies

While working on my NextJS project, I made the decision to utilize yarn as my package manager and utilized yarn link for import aliases/absolute imports. This feature of yarn is quite handy and is recommended for managing aliases within a project. However, ...