Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    136
    Thanks
    0
    Thanked 0 Times in 0 Posts

    counting with buttons

    Please keep in mind that I know very little of javascript.

    I found this code:
    Code:
    <html>
    <head>
    <script>
    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;
    }
    </script>
    </head>
    <body>
    
    <form name="myform">
     <input type="button" value="-" onclick="sub()" />
     <span id="mycnt">0</span>
     <input type="button" value=" + " onclick="add()" />
    </form>
    
    </body>
    </html>
    It is exactly what I am needing to do, but I need multiple of them all the same page. Whenever I add another form with the buttons, of course, it breaks since the script only will call the "myform" form.

    Since I am going to be needing about 50 of these on the same page, I dont want the javascript to be huge.

    How can I do this? Please please help. It is very much appreciated!

    -Brad

  • #2
    New Coder
    Join Date
    Jul 2005
    Posts
    67
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <HTML>
    <Head>
    <Script Language=JavaScript>
    
    	function update(isField,toSum){
    
    		currCount = parseInt(isField.value);
    		newCount = currCount+toSum;
    		isField.value = newCount;
    	}
    	
    
    </Script>
    </Head>
    <Body>
    <Form name='counters'>
    Counter 1:
    <input type=button value="+" onclick="update(this.form.counter1,1)">
    <input type=button value="-" onclick="update(this.form.counter1,-1)">
    <input type=text size=10 name='counter1' value=0 readonly>
    <br>
    Counter 2:
    <input type=button value="+" onclick="update(this.form.counter2,1)">
    <input type=button value="-" onclick="update(this.form.counter2,-1)">
    <input type=text size=10 name='counter2' value=0 readonly>
    <br>
    Counter 3:
    <input type=button value="+" onclick="update(this.form.counter3,1)">
    <input type=button value="-" onclick="update(this.form.counter3,-1)">
    <input type=text size=10 name='counter3' value=0 readonly>
    </Form>
    </Body>
    </HTML>

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    136
    Thanks
    0
    Thanked 0 Times in 0 Posts
    YOU ARE MY HERO!!!!!

    Thank you SOOOOO much!

  • #4
    New Coder
    Join Date
    Jul 2005
    Posts
    67
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm nothing of the sort, but you are welcome.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •