Quotes help make search much faster. Example: "Practice Makes Perfect"

Monday, December 17, 2012

Codecademy Sample Solution: Codeyear Hook Up the Buttons



<html>
    <head>
        <script type="text/javascript" src="script.js"></script>
    </head>
    <body>
        <label for="qty">Quantity</label>
        <input id="qty" value="0" />
        <button onclick="modify_qty(-1)">-1</button>
        <button onclick="modify_qty(1)">+1</button>
    </body>
</html>

// sample solution to script.js

function modify_qty(val) {
    var qty = parseFloat(document.getElementById('qty').value);
    var new_qty = qty + val;
   
    if (new_qty < 0) {
        new_qty = 0;
    }
   
    document.getElementById('qty').value = parseFloat(new_qty);
    return new_qty;
}

No comments:

Post a Comment


This is an example of scrolling text using Javascript.

Popular Posts