Web Analytics Made Easy -
StatCounter Combining Text and numeric variables - CodingForum

Announcement

Collapse
No announcement yet.

Combining Text and numeric variables

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Combining Text and numeric variables

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

  • #2
    This is probably not a good answer
    as i really dont understand the question ....

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

    Comment


    • #3
      Originally posted by lilwillywonka View Post
      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:

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

      Code:
      <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.)

      Comment

      Working...
      X
      😀
      🥰
      🤢
      😎
      😡
      👍
      👎