Clear value of an id with jquery

Asked

Viewed 103 times

0

Colleagues.

I have a select that is 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 amount of the button below:

inserir a descrição da imagem aqui

Which is represented as follows:

$('.value-plus1').on('click', function(){
    var estoqueProduto = 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 when selecting the value 3 and then the value 2, the value 3 continues to appear. I would like that when changing the value of the stock, the quantity number returns to the number 1.

1 answer

2


You can do as follows: whenever the product select is changed 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>

<input id="quantidade" value="3">

  • Thank you Jessika.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.