Counting With Buttons2. Plain Text |
|
Join the DiscussionMore of this FeatureBy the addition of a whole lot more code we can get rid of the need to have the count enclosed in an input field. This version should work in all current browsers but may not function in some earlier ones. Here is an example: Here's the Javascript code (with the additions and changes shown in bold):
var aDOM = 0, ieDOM = 0, nsDOM = 0
var stdDOM = document.getElementById; if (stdDOM) aDOM = 1; else { ieDOM = document.all; if (ieDOM) aDOM = 1; else { var nsDOM = ( (navigator.appName.indexOf('Netscape') != -1) && (parseInt(navigator.appVersion) ==4)); if (nsDOM) aDOM = 1; } } function xDOM(objectId) { if (stdDOM) return (document.getElementById(objectId)); if (ieDOM) return (document.all[objectId]); if (nsDOM) return (document.layers[objectId]); } var cnt = 0; function add() {cnt++;set();} function sub() {cnt--;set();} function set() { var obj = xDOM('mycnt'); obj.innerHTML = cnt; } And here is the amended HTML (again with the changes shown in bold):
<form name="myform">
<input type="button" value="-" onclick="sub()" /> <span id="mycnt">0</span> <input type="button" value=" + " onclick="add()" /> </form> Input Field | Plain Text
|

