Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2008
    Location
    Here
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Populating textboxes with arrays

    In my webpage there 3 text boxes (txt1, txt2, txt3) & a button. My Javascript has 2 arrays:
    var s = new Array(1, 2);
    var m = new Array(3, 4);

    How do I do the Javascript & HTML so that if I enter 's' into txt1 & click the button the values '1' & '2' in the array shows in txt2 & txt3? Same with 'm' array

    I tried:
    Code:
    <HTML>
    <HEAD>
    <TITLE>JavaScript</TITLE>
    <script LANGUAGE="JAVASCRIPT">
    var data = {
      s : [1, 2],
      m : [3, 4]
    }
    
    function foo(){
    
      var userInput = document.forms[0].txt1.value;
      var vals = data[userInput];
      var val1="";
      var val2="";
    
      if(vals==null){
        val1 = vals[0];
        val2 = vals[1];
      }
    
      document.forms[0].txt2.value = val1;
      document.forms[0].txt3.value = val2;
    
    }
    </script>
    </HEAD>
    <BODY bgcolor="blue">
    <input type = text name=txt1 id="txt1" value="" /><br>
    <input type="button" value="button" name="button" onclick="return foo()"/><br>
    <input type = text name=txt2 id="txt2" value="" /><br>
    <input type = text name=txt3 id="txt3" value="" />
    </BODY>
    </HTML>
    But why do I get this error:
    'document.forms.0.txt1 is null or not an object'

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    why did you start a new thread? No reason for that.

    As I said in the other post, you need a form. You do not have a form tag. Also there is a typo in my original code.

    vals==null

    should be

    vals!=null

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •