Jump to content

object name with variable how to?


tomasd

Recommended Posts

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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

Thanks very much, it worked :D

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?

Link to comment
Share on other sites

<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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.