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 Coder
    Join Date
    Apr 2008
    Posts
    26
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Javascript Array Help

    I needed some help with an array. Right now here is my code that works but is very insufficient as I am still learning JS.

    Code:
    <script type="text/javascript">
    function updateform(fieldinfo){
    	selecteditem = fieldinfo.newinfo.selectedIndex ;
    	fieldnumber = fieldinfo.newinfo.options[ selecteditem ].value ;
    	if(fieldnumber==1){
    		document.getElementById('name').innerHTML = '<input name="name" type="text" value="Name 1" size="20" maxlength="100" />';
    		document.getElementById('email').innerHTML ='<input name="email" type="text" value="name1@email.com" size="20" maxlength="100" />';
    	} else if(fieldnumber==2){
    		document.getElementById('name').innerHTML = '<input name="name" type="text" value="Name 2" size="20" maxlength="100" />';
    		document.getElementById('email').innerHTML ='<input name="email" type="text" value="name2@email.com" size="20" maxlength="100" />';
    	}
    }
    </script>
    <span id="name"><input name="name" type="text" value="" size="20" maxlength="100" /></span>
    <span id="email"><input name="email" type="text" value="" size="20" maxlength="100" /></span>
    
    
    <form action="null">
    <select name="newinfo" onchange="updateform(this.form)">
    	<option selected="selected">Select Member</option>
    	<option value="1">Name 1</option>
    	<option value="2">Name 2</option>
    </select>
    </form>
    What the code does is when the user selects a name, it auto fills the name and email in the text boxes. Can someone tell me how I can make an array so that I dont have to repeat the 3 lines of code for every member? If there is a better way than what I have posted to accomplish this task I am open to ideas aswell.

    Thanks!


    EDIT

    After some research I managed to get an array working.

    Code:
    <script type="text/javascript">
    function updateform(fieldinfo){
    	selecteditem = fieldinfo.newinfo.selectedIndex ;
    	fieldnumber = fieldinfo.newinfo.options[ selecteditem ].value ;
    	
    	var mynames=new Array(2);
    	mynames[0]="";
    	mynames[1]="Name 1";
    	mynames[2]="Name 2";
    
    	var myemails=new Array(2);
    	myemails[0]="";
    	myemails[1]="name1@email.com";
    	myemails[2]="name2@email.com";
    
    	document.getElementById('name').innerHTML = '<input name="name" type="text" value="' + mynames[fieldnumber] + '" size="20" maxlength="100" />';
    	document.getElementById('email').innerHTML ='<input name="email" type="text" value="' + myemails[fieldnumber] + '" size="20" maxlength="100" />';
    }
    </script>
    <span id="name"><input name="name" type="text" value="" size="20" maxlength="100" /></span>
    <span id="email"><input name="email" type="text" value="" size="20" maxlength="100" /></span>
    
    
    <form action="null">
    <select name="newinfo" onchange="updateform(this.form)">
    	<option selected="selected" value="0">Select Member</option>
    	<option value="1">Name 1</option>
    	<option value="2">Name 2</option>
    </select>
    </form>
    Is there a better way than what I have posted?

    Thanks again.
    Last edited by pixencom; 04-30-2008 at 05:35 PM.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,858
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Instead of the innerHTML, use some DOM methods like
    Code:
    <input name="name" id="name" type="text" value="" size="20" maxlength="100" />
    <input name="email" id="email" type="text" value="" size="20" maxlength="100" />
    Code:
    <select name="newinfo" onchange="updateform(this.value)">
    	<option selected="selected">Select Member</option>
    	<option value="1">Name 1</option>
    	<option value="2">Name 2</option>
    </select>
    Code:
    function updateform(value){
    var name= document.getElementById('name');
    var email= document.getElementById('name');
    
    name.value='Name '+value;
    email.value='name'+value+'@email.com'
    }
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

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