简体   繁体   中英

unable to increase value using + operator in javascript

I am trying to increment value by 1 whenever the button is clicked. I even convert the value to int by using parseInt() by it keep increasing the counter but not the value.

when I clicked the button whose value is 1. My expected result will be on click is 2 3 4 5 6 7 and so on...

<div id="move_left_right">
 <button class="btn" value=1 onclick="counter()" id="left"><<</button>
</div>


function counter()
{
    let valueIs  = document.getElementById('left').value;
    let conversion = parseInt(valueIs);
    let sum = conversion + 1;
    localStorage.setItem("getSum" , sum);
}
//getting value of sum outside function using local storage
let x = localStorage.getItem("getSum")
console.log(x);
//setting value to left as 2 
document.getElementById('left').value= x;

//now value is 2 when button is pressed i want the value to be 3 4 5 etc

I edited you function ,now it updates the button's value + it stores in into the local storage :

function counter() {
    let btn =  document.getElementById('left')
    let value = parseInt(btn.value);
    let sum = value + 1;
    localStorage.setItem("getSum" , sum);
    btn.value = sum;

    console.log(sum);
    console.log(typeof(sum));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM