Plus/Minus jquery does not work

Asked

Viewed 76 times

2

I am unable to change the input value id="value" when you click on .Minus or .plus, what I must do to resolve?

<input type="button" value="+" class="plus">                                                

<input  id="value" step="1" max="99" min="1" title="Qty" class="qty" type="number" name="prod[<?php echo $result['id']?>]" value="<?php echo $result['quantity']?>" size="4" />

<input type="button" value="-" class="minus">

jquery

$('.plus').click(function() { changeValue(1); });
$('.minus').click(function() { changeValue(-1); });

function changeValue(val) {
    var container = $('#value');
    var current = parseInt(container.html(), 10);

    container.html(Math.max(0, current + val).toString());
}

1 answer

0


Change container.html() for container.val(). The .html() serves to take the internal HTML of the element and not the value. O .val() yes will take the value of input.

$('.plus').click(function() { changeValue(1); });
$('.minus').click(function() { changeValue(-1); });

function changeValue(val) {
    var container = $('#value');
    var current = parseInt(container.val(), 10);
    container.val(Math.max(0, current + val).toString());
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" value="+" class="plus">                                                

<input  id="value" step="1" max="99" min="1" title="Qty" class="qty" type="number" name="nome" value="3" size="4">

<input type="button" value="-" class="minus">

Browser other questions tagged

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