HTMLJavaScript - Up and Down buttons for quantity not working - Stack Overflow

I'm trying to create buttons which increments or decrements a quantity value.HTML:<div class=&q

I'm trying to create buttons which increments or decrements a quantity value.

HTML:

<div class="order-option">
    Quantity:
    <span id="quantity-field">
        <button id="up" onclick="setQuantity('up');">+</button>
        <input type="text" id="quantity" value="1">
        <button id="down" onclick="setQuantity('down');">-</button>
    </span>
</div>

JavaScript:

function setQuantity(upordown) {
    var quantity = document.getElementById('quantity');

    if (quantity.value > 1) {
        if (upordown == 'up'){++document.getElementById('quantity').value;}
        else if (upordown == 'down'){--document.getElementById('quantity').value;}}
    else if (quantity.value == 1) {
        if (upordown == 'up'){++document.getElementById('quantity').value;}}
    else
        {document.getElementById('quantity').value=1;}
}

It's pretty cut and dry. The function is passed 'up' or 'down' depending on which button is clicked, then decides what to do based on the current value of the quantity element. Unfortunately, it's not doing a thing, and I can't figure out why. Any help would be much appreciated.

I'm trying to create buttons which increments or decrements a quantity value.

HTML:

<div class="order-option">
    Quantity:
    <span id="quantity-field">
        <button id="up" onclick="setQuantity('up');">+</button>
        <input type="text" id="quantity" value="1">
        <button id="down" onclick="setQuantity('down');">-</button>
    </span>
</div>

JavaScript:

function setQuantity(upordown) {
    var quantity = document.getElementById('quantity');

    if (quantity.value > 1) {
        if (upordown == 'up'){++document.getElementById('quantity').value;}
        else if (upordown == 'down'){--document.getElementById('quantity').value;}}
    else if (quantity.value == 1) {
        if (upordown == 'up'){++document.getElementById('quantity').value;}}
    else
        {document.getElementById('quantity').value=1;}
}

It's pretty cut and dry. The function is passed 'up' or 'down' depending on which button is clicked, then decides what to do based on the current value of the quantity element. Unfortunately, it's not doing a thing, and I can't figure out why. Any help would be much appreciated.

Share Improve this question asked Feb 7, 2013 at 22:03 Todd BauerTodd Bauer 2191 gold badge7 silver badges15 bronze badges 1
  • 4 Can we see a Fiddle? – Mooseman Commented Feb 7, 2013 at 22:04
Add a ment  | 

2 Answers 2

Reset to default 4

I went ahead and pasted the code into a fiddle, and got the console error that at the time of onclick, setQuantity was not defined. Making sure that the function is declared before the markup that calls it solves the problem for me: http://jsfiddle/KR2Az/

As alluded to by crowjonah, your javascript should ideally appear in the <HEAD> of the page.

I also suggest separating the javascript from your HTML like this:

<script>
quantity = document.getElementById('quantity');

button_up=document.getElementById('up');
button_down=document.getElementById('down');

button_up.onclick=function() {setQuantity('up');}
button_down.onclick=function() {setQuantity('down');}

function setQuantity(upordown) {
    var quantity = document.getElementById('quantity');

    if (quantity.value > 1) {
        if (upordown == 'up'){++quantity.value;}
        else if (upordown == 'down'){--quantity.value;}}
    else if (quantity.value == 1) {
        if (upordown == 'up'){++quantity.value;}}
    else
        {quantity.value=1;}
}
</script>


<div class="order-option">
    Quantity:
    <span id="quantity-field">
        <button id="up">+</button>
        <input type="text" id="quantity" value="1">
        <button id="down">-</button>
    </span>
</div>

jFiddle here

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744655461a4586152.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信