Plus / minus buttons

by webgleb

HTML

<div id="field1">
    <button type="button" id="sub" class="sub">-</button>
    <input type="text" id="1" value="0" class="field" />
    <button type="button" id="add" class="add">+</button>
</div>
<div id="field2">
    <button type="button" id="sub2" class="sub">-</button>
    <input type="text" id="2" value="0" class="field" />
    <button type="button" id="add2" class="add">+</button>
</div>

CSS

button {
    margin: 4px;
    cursor: pointer;
    outline: none;
    border: 0;
    background: transparent;
    font-size: 1em;
    font-weight: 600;
}
input {
    text-align: center;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    margin: 4px;
    background: yellow;
    color: black;
    font-size: 1.5em;
}

JavaScript

$('.add').click(function () {
    $(this).prev().val(+$(this).prev().val() + 1);
});
$('.sub').click(function () {
    if ($(this).next().val() > 0) $(this).next().val(+$(this).next().val() - 1);
});