Clear value of an id with jquery

0

Colleagues.

I have a select from which you are bringing the stocks from a database. I'm capturing it this way:

<select name='estoque' id='estoqueProdutos'>
 <option value='1'>1</option>
 <option value='2'>2</option>
 <option value='3'>3</option>
</select>

JQuery

function alterar(){
    var estoques = $('#estoqueProdutos').val();
    var estoque = document.getElementById("estoque").value = estoques;
}   

Each stock will limit the quantity of the button below:

Itisrepresentedasfollows:

$('.value-plus1').on('click',function(){varestoqueProduto=document.getElementById("estoque").value;

    var divUpd = $(this).parent().find('.value1'), newVal = parseInt(divUpd.text(), 10)+1;                                      
    if(newVal <= estoqueProduto){
       document.getElementById("quantidade").value = newVal;
       divUpd.text(newVal);
    }   
});
$('.value-minus1').on('click', function(){
    var divUpd = $(this).parent().find('.value1'), newVal = parseInt(divUpd.text(), 10)-1;                                      
    if(newVal>=1){ 
        divUpd.text(newVal); 
        document.getElementById("quantidade").value = newVal;
    }
});

So far so good, the problem is that by selecting the value 3 and then the value 2, the value 3 continues to appear. I would like you to change the stock value, the quantity number back to the number 1.

    
asked by anonymous 06.04.2017 / 19:21

1 answer

2

You can do this as follows: whenever you select the products you change, you change the quantity value to 1. Example:

$('#estoqueProdutos').on('change', function()
{
    $('#quantidade').val('1')
});
<select name='estoque' id='estoqueProdutos'>
 <option value='1'>1</option>
 <option value='2'>2</option>
 <option value='3'>3</option>
</select>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><inputid="quantidade" value="3">
    
06.04.2017 / 19:37