...

View Full Version : Javascript Array Help



pixencom
04-30-2008, 05:27 PM
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.



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



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

abduraooft
04-30-2008, 05:40 PM
Instead of the innerHTML, use some DOM methods like

<input name="name" id="name" type="text" value="" size="20" maxlength="100" />
<input name="email" id="email" type="text" value="" size="20" maxlength="100" />


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



function updateform(value){
var name= document.getElementById('name');
var email= document.getElementById('name');

name.value='Name '+value;
email.value='name'+value+'@email.com'
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum