...

View Full Version : Combining Text and numeric variables



lilwillywonka
09-05-2011, 11:25 PM
I would like to fill a text field using both text and variables..

i.e. it would say x+2, so i would write.. (if 2 were a given variable)
form.form1.value = "x+" 2 ???

(I know that isn't correct, I'm just wondering how I would go about this..)

DaveyErwin
09-06-2011, 12:04 AM
This is probably not a good answer
as i really dont understand the question ....


<form name="frm">
<input name="a" >
<script>var a=2*1;frm.a.value="2+" + a </script>
<input name= "b" >
<script>frm.b.value=eval("2+" + a ) </script>
</form>

input a has 2 + 2
input b has 4

Arbitrator
09-06-2011, 02:46 AM
I would like to fill a text field using both text and variables..

i.e. it would say x+2, so i would write.. (if 2 were a given variable)
form.form1.value = "x+" 2 ???

(I know that isn't correct, I'm just wondering how I would go about this..)If you want the text field to read "x + #" where # is a numeric variable, you would use something like this:


var input_el = document.getElementById("text_field");
var my_numeric_variable = 2;
input_el.value = "x + " + my_numeric_variable.toString();

That would operate on HTML like this:


<input id="text_field" type="text">

(Note that .toString() isn't technically required since JavaScript automatically converts variable types (here from a number to a string), but I prefer to do these conversions explicitly instead of relying on "magic" to auto-convert between types.)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum