tomasd Posted March 17, 2009 Share Posted March 17, 2009 Hi, I've got a little code which increments/decrements a value from the form: function increment(){ z=z+1; document.order.Button1.value=z; } function decrement(){ if (document.order.Button1.value>0){ z=z-1; document.order.Button1.value=z; } } If there any way of passing something else to the function (not Button1) for example: function increment(var){ z=z+1; document.order.var.value=z; } function decrement(var){ if (document.order.var.value>0){ z=z-1; document.order.var.value=z; } } Thanks very much! Quote Link to comment Share on other sites More sharing options...
rhodesa Posted March 17, 2009 Share Posted March 17, 2009 document.order[var].value=z; Quote Link to comment Share on other sites More sharing options...
tomasd Posted March 17, 2009 Author Share Posted March 17, 2009 document.order[var].value=z; Thanks for quick reply, I did this: <script language="JavaScript"> function increment(test){ z=z+1; document.order[test].value=z; } </script> <script language="JavaScript"> z=0; </script> <form name="order"> <input type="button" value="+" onclick="increment(test)"> <input type="text" name="test" size=1 value=0> </form> And that is no joy am I doing something wrong? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted March 17, 2009 Share Posted March 17, 2009 single quotes around test: <input type="button" value="+" onclick="increment('test')"> Quote Link to comment Share on other sites More sharing options...
tomasd Posted March 18, 2009 Author Share Posted March 18, 2009 Thanks very much, it worked I ran into another challenge now ??? If let's say I use this: <script language="JavaScript"> function increment(name){ z=z+1; document.order[name].value=z; } </script> <script language="JavaScript"> z=0; </script> <form name="order"> <input type="button" value="+" onclick="increment('test1');"> <input type="text" name="test1" size=1 value=0> <input type="button" value="+" onclick="increment('test2');"> <input type="text" name="test2" size=1 value=0> </form> What happens is that I'm using same z value for both test1 and test2 input fields and if I increment one and then increment another it takes value from the first and then increments to the second is there any way of making them to increment independently? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted March 18, 2009 Share Posted March 18, 2009 <script type="text/javascript"> function increment(name){ var i = new Number(document.order[name].value); document.order[name].value = i + 1; } </script> <form name="order"> <input type="button" value="+" onclick="increment('test1');"> <input type="text" name="test1" size=1 value=0> <input type="button" value="+" onclick="increment('test2');"> <input type="text" name="test2" size=1 value=0> </form> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.