最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to create a counter with a plus and minus button which passes a parameter so that I don't have two use

programmeradmin2浏览0评论

I have created a counter with a plus and minus button. The counter works fine but I would like to simplify my code by passing a parameter to my function that performs both the plus and minus calculations depending on which button is clicked. At the moment I have two separate functions doing this but I would like to pass the 'result' variable as this is the part that tells my script whether I want to add 1 or minus 1, however I am unsure on how to do this?

I have attached my JavaScript below to show what I have so far.

document.getElementById('minus').onclick = function() {
    var counter = document.getElementById('counter').innerHTML;
    var parsed = parseInt(counter);
    var result = parsed -1;
    document.getElementById('counter').innerHTML = result;
}

document.getElementById('plus').onclick = function() {
    var counter = document.getElementById('counter').innerHTML;
    var parsed = parseInt(counter);
    var result = parsed +1;
    document.getElementById('counter').innerHTML = result;
}

I have created a counter with a plus and minus button. The counter works fine but I would like to simplify my code by passing a parameter to my function that performs both the plus and minus calculations depending on which button is clicked. At the moment I have two separate functions doing this but I would like to pass the 'result' variable as this is the part that tells my script whether I want to add 1 or minus 1, however I am unsure on how to do this?

I have attached my JavaScript below to show what I have so far.

document.getElementById('minus').onclick = function() {
    var counter = document.getElementById('counter').innerHTML;
    var parsed = parseInt(counter);
    var result = parsed -1;
    document.getElementById('counter').innerHTML = result;
}

document.getElementById('plus').onclick = function() {
    var counter = document.getElementById('counter').innerHTML;
    var parsed = parseInt(counter);
    var result = parsed +1;
    document.getElementById('counter').innerHTML = result;
}
Share Improve this question asked Sep 9, 2019 at 11:34 Hannah MatthewsonHannah Matthewson 514 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 3

You can make a curried function to "bake" your delta value into the click handlers:

function changeCount(delta) {
   return function () {
       var counter = document.getElementById('counter').innerHTML;
       var parsed = parseInt(counter);
       var result = parsed + delta;
       document.getElementById('counter').innerHTML = result;
   } 
}

document.getElementById('minus').onclick = changeCount(-1);
document.getElementById('plus').onclick = changeCount(1);

You can use the same function to modify the counter element and then just pass a negative or positive integer as the parameter to the function, like so:

document.getElementById('minus').onclick = modifyCount(-1);

document.getElementById('plus').onclick = modifyCount(1); 

//Just pass the integer into the function to modify the counter element
function modifyCount(val){
    const counter = document.getElementById('counter');
    counter.innerHTML = parseInt(counter.innerHTML) + val;
}

Possible solution with onclick event:

  function count(clicked_id)
  {
  var counter = document.getElementById('counter').innerHTML;
    var parsed = parseInt(counter);
      let result = clicked_id == "minus" ? parsed - 1 : parsed + 1;
      document.getElementById('counter').innerHTML = result;
  }
<button onclick="count(this.id)" id="minus">-</button>
<div id="counter">0</div>
<button onclick="count(this.id)" id="plus">+</button>

You can use the first and only parameter of the click handler, to get the ID of the element. Then use a ternary mand to decide, if the result should be incremented or decremented.

function clickHandler(e) {
    var counter = document.getElementById('counter').innerHTML;
    var parsed = parseInt(counter);
    var result = e.target.id === 'plus' ? parsed + 1 : parsed - 1;
    document.getElementById('counter').innerHTML = result;
}

document.getElementById('minus').onclick = clickHandler;
document.getElementById('plus').onclick = clickHandler;

You could also rewrite the method to use an if instead of the result variable.

Here's what I would consider an optimized version:

const counterElem = document.getElementById('counter');

function clickHandler(e) {
    counterElem.innerHTML =
        parseInt(counterElem.innerHTML) +
        (e.target.id === 'plus' ? 1 : -1);
}

document.getElementById('minus').onclick = clickHandler;
document.getElementById('plus').onclick = clickHandler;

You could use curried functions.

The following should make your code simple and as required:

function getCounter(multiplier = 1) {
    return function () {
       var counter = document.getElementById('counter').innerHTML;
       var parsed = parseInt(counter);
       var result = parsed + (multiplier * 1);
       document.getElementById('counter').innerHTML = result;
    }
}

document.getElementById('minus').onclick = getCounter(-1);

document.getElementById('plus').onclick = getCounter(); // 1 is the default value

Curried functions are basically, functions that return another function. The inner functions have access to the variables defined in the wrapping function. read more about them here: https://medium./javascript-scene/curry-and-function-position-2c208d774983

I have modified your code to a function, you just need to use that function on the event you want.

function counterFunction(action) {
 var counter = document.getElementById('counter').innerHTML;
 var parsed = parseInt(counter);
 var result = '';
  if (action == 'minus') {
    result = parsed -1;
  }
  else if (action == 'plus') {
    result = parsed +1;
  }
  document.getElementById('counter').innerHTML = result;
}

If you need any help please let me know.

  1. In Button tag call the below method in OnClick event
  2. In 'action' parameter , pass the button value as '+' or '-'.

Eg : <button type='button' onClick='return CounterUpdate("+")'>+</Button>

function CounterUpdate(action) {
    var counter = document.getElementById('counter').innerHTML;
    var parsed = parseInt(counter);
    var result =parsed ;
    if(action=='+')
    {
      result = parsed +1;
    }
    else if(action=='-')
    {
      result = parsed -1;
    }
    document.getElementById('counter').innerHTML = result;
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论