How do I pass the value of the previous form element into an "onchange" javascript function?

Posted by Jen on Stack Overflow See other posts from Stack Overflow or by Jen
Published on 2009-10-01T04:43:11Z Indexed on 2010/05/03 5:08 UTC
Read the original article Hit count: 226

Hello,

I want to make some UI improvements to a page I am developing. Specifically, I need to add another drop down menu to allow the user to filter results.

This is my current code: HTML file:

<select name="test_id" onchange="showGrid(this.name, this.value, 'gettestgrid')">
<option selected>Select a test--></option>
<option value=1>Test 1</option>
<option value=2>Test 2</option>
<option value=3>Test 3</option>
</select>

This is pseudo code for what I want to happen:

<select name="test_id">
<option selected>Select a test--></option>
<option value=1>Test 1</option>
<option value=2>Test 2</option>
<option value=3>Test 3</option>
</select>
<select name="statistics" onchange="showGrid(PREVIOUS.name, PREVIOUS.VALUE, THIS.value)">
<option selected>Select a data display --></option>
<option value='gettestgrid'>Show averages by student</option>
<option value='gethomeroomgrid'>Show averages by homeroom</option>
<option value='getschoolgrid'>Show averages by school</option>
</select>

How do I access the previous field's name and value? Any help much appreciated, thx!

Also, JS function for reference:

function showGrid(name, value, phpfile)
{ 
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url=phpfile+".php"; 
url=url+"?"+name+"="+value;
url=url+"&sid="+Math.random();

xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about javascript-events