I have a form that looks something like this:
[code=html]<input type=’text’ id=’line1′ onChange=’getUnits(1);’>
<input type=’text’ id=’line2′ onChange=’getUnits(2);’>
<input type=’text’ id=’line3′ onChange=’getUnits(3);’>
Whenever new data is put into a text box, I want a corresponding box to receive a value from an asynchronous request. I’m a little confused how I get this to happen though, because the code below (provided by yahoo), sets the ‘div’ variable outside of the getUnits() function. I suppose I could have 30 copies of the code, but that seems a little… bloated. If you aren’t familiar with the yahoo stuff, basically servelet.php will pass back some info, and it gets inserted into the innerHTML of ‘div’. This works just fine if I only have one line, but there could be an arbitrary number.
[CODE]function getUnits(line_number) {
s = document.getElementById(‘line’+line_number).value;
e = document.getElementById(‘line’+line_number).value;
var transaction = YAHOO.util.Connect.asyncRequest(‘GET’, ‘servelet.php?s=’+s+’&e=’+e+’&req=freeUnits’, callback);
}
var div = document.getElementById(‘units’); <– I want this to be units_1, units_2, etc…
var handleSuccess = function(o){
if(o.responseText !== undefined){
div.innerHTML = o.responseText;
}
}
var handleFailure = function(o){
if(o.responseText !== undefined){
div.innerHTML = “oh crap…”;
}
}
var callback =
{ success:handleSuccess,
failure: handleFailure
};
Am I missing something obvious?