Javascript add value if checkbox is checked -
<p> <label for="hours">learn javascript hardy way’ - complete e-book (printed)</label> <input type="number" min="0" id="chap7" name="hours" value="0"> </p> <input type="checkbox" id="printed"/>please tick if printed version<br /> <p class="post">postage : <strong>£<output id="post">0</output></strong><p>
i piece of javascript upon checkbox being checked, makes postage value 3.50. however, 3.50 per copy of complete ebook. have check value of number box , times value inside.
solution changes based on checkbox state:
var chap7 = document.getelementbyid('chap7'), post = document.getelementbyid('post'), printed = document.getelementbyid('printed'); printed.addeventlistener('change', function() { var quantity = parseint(chap7.value, 10); post.value = printed.checked ? quantity * 3.5 : quantity; }, false); chap7.addeventlistener('change', function() { post.value = parseint(this.value, 10); });
<p> <label for="hours">learn javascript hardy way’ - complete e-book (printed)</label> <input type="number" min="0" id="chap7" name="hours" value="0"> </p> <input type="checkbox" id="printed"/>please tick if printed version<br /> <p class="post">postage : <strong>£<output id="post">0</output></strong><p>
solution changes based on text input state:
var chap7 = document.getelementbyid('chap7'), post = document.getelementbyid('post'); chap7.addeventlistener('change', function(e) { var quantity = parseint(chap7.value, 10); post.value = quantity * 3.5; }, false);
<p> <label for="hours">learn javascript hardy way’ - complete e-book (printed)</label> <input type="number" min="0" id="chap7" name="hours" value="0"> </p> <p class="post">postage : <strong>£<output id="post">0</output></strong><p>
Comments
Post a Comment