Solved

Javascript issue with document.forms function

Posted on 2011-09-07
5
348 Views
Last Modified: 2012-06-21
I'm having an issue with the document.forms function in this java-script , but I cannot seem to find the issue.  This is my first foray into any real java-script.   the first drop down to field translation works correctly, but none of the additional fields will auto populate with the defined value.  Any help would be greatly appreciated.

<head>
<title>TEST</title>
</head>

<body>

<script type="text/javascript">
if(window.addEventListener){
window.addEventListener('load',switchme,false);
}
else { 
if(window.attachEvent){
window.attachEvent('onload',switchme);
}
}

function switchme(){
df=document.forms[0];
df[0].onchange=function(){
switch(this.value) {
case 'A':
df[1].value='Item  1';
break;
case 'B':
df[1].value='Item  2';
break;
case 'C':
df[1].value='Item  3';
break;
case 'D':
df[1].value='Option 1';
break;
case 'E':
df[1].value='Option 2';
break;
default:
df[1].value='';
break;
}
}
}
</script>

<form action="#" method="get">
<div>
	<table align="center" style="width: 500px">
		<tr>
			<td>
<select name="1type">
<option value="[none]" selected="selected">---</option>
	<option value="A">Item</option>
	<option value="D">Option ?</option>
	<option value="E">Option ?</option>
</select></td>
			<td>
<input name="1value" type="text"/>
			</td>
		</tr>
		<tr>
			<td>
<select name="2type">
<option value="[none]" selected="selected">---</option>
	<option value="B">Item</option>
	<option value="D">Option ?</option>
	<option value="E">Option ?</option>
</select></td>
			<td>
<input name="2value" type="text"/>
			</td>
		</tr>
		<tr>
			<td>
<select name="3type">
<option value="[none]" selected="selected">---</option>
	<option value="C">Item</option>
	<option value="D">Option ?</option>
	<option value="E">Option ?</option>
</select></td>
			<td>
<input name="3value" type="text"/>
			</td>
		</tr>
	</table>
</div>
</form>

</body>

Open in new window

0
Comment
Question by:mcarter1953
  • 2
  • 2
5 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 36499535
the onchange attribute is only set to the first dropdown (form element with index 0)
the first dropdown have options with the following value : A,D and E

your switch only change the value of the second textbox (form element with index 1)

so everything run fine
if you want to handle onchange event for the other dropdown set :
df[2].onchange (the second dropdown)
and
df[4].onchange (the third dropdown)
0
 
LVL 82

Expert Comment

by:hielo
ID: 36499547
document.forms[0]

refers to the first <FORM> in your document. From what I can guess, what you are interested in is in the onchange event of the SELECT lists:
function switchme(){
	var df=document.forms[0].getElementsByTagName('SELECT');
	df[0].onchange=function(){
		alert( 'selected ' + this.value )

//it's not clear to me what you are trying to do in the switch
//otherwise I would offer more help
		switch(this.value) {
		case 'A':
			df[1].value='Item  1';
			break;
		case 'B':
			df[1].value='Item  2';
			break;
		case 'C':
			df[1].value='Item  3';
			break;
		case 'D':
			df[1].value='Option 1';
			break;
		case 'E':
			df[1].value='Option 2';
			break;
		default:
			df[1].value='';
			break;
		}
	}
}

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 36499622
read comments in code and try it:
function switchme(){
	//here f will hold a reference to the <FORM> object
	var f=document.forms[0];

	//get an array of your selects
	var selectList=f.getElementsByTagName('SELECT');

	//iterate through list and attach the onchange to each of them
	for(var i=0,limit=selectList.length; i < limit; ++i)
	{
		selectList[i].onchange=function(){
			//when your selected item is from list name="1type", then prefix will have 1
			//when your selected item is from list name="2type", then prefix will have 2
			//when your selected item is from list name="3type", then prefix will have 3
			var prefix=this.name.charAt(0);

    		switch(this.value) {
	    		case 'A':
					//again f is a reference to <FORM>
					//since the form contains <input name="1value"..>
					//you can access/reference THAT specific input with:
					//	f['1value']
					//If you want to set the value of THAT input to say 3, then 
					//just append the ".value" attribute and assign it 3
					//	f['1value'].value=3
					//Knowing that, you can then just dynamically "construct" the "1value"
					//by using the prefix variable above, which leads you to the statement below
					//and now it works for all the selects
    				f[prefix+'value'].value='Item  1';

    				break;

					//the comments in case 'A' apply to the rest
    			case 'B':
    				f[prefix+'value'].value='Item  2';
	    			break;
    			case 'C':
    				f[prefix+'value'].value='Item  3';
    				break;
	    		case 'D':
    				f[ prefix+'value'].value='Option 1';
    				break;
    			case 'E':
    				f[prefix+'value'].value='Option 2';
	    			break;
    			default:
    				f[prefix+'value'].value='';
    				break;
    		}
		}
	}
}

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36499634
Check this : http://jsfiddle.net/QUvd5/
if(window.addEventListener){
    window.addEventListener('load',switchme,false);
}
else { 
    if(window.attachEvent){
        window.attachEvent('onload',switchme);
    }
}

function switchme(){
    df=document.forms[0];
    df[0].onchange = new Function("switchx(this.value,1)");
    df[2].onchange = new Function("switchx(this.value,3)");
    df[4].onchange = new Function("switchx(this.value,5)");
}

function switchx(x,n){
    switch(x) {
        case 'A':
            df[n].value='Item  1';
            break;
        case 'B':
            df[n].value='Item  2';
            break;
        case 'C':
            df[n].value='Item  3';
            break;
        case 'D':
            df[n].value='Option 1';
            break;
        case 'E':
            df[n].value='Option 2';
            break;
        default:
            df[n].value='';
            break;
    }
}

Open in new window

0
 

Author Closing Comment

by:mcarter1953
ID: 36499664
thanks for the help, great fix :)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Showing your events from Google Calendar in Google Maps Why? I travel all week and I thought it would be ideal if staff in office knew where I was based on my calendar. (OK real reason: my son wanted to see where I would be working, and I thoug…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now