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
https://forums.phpfreaks.com/topic/149847-object-name-with-variable-how-to/
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?

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?

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.