/    Sign up×
Community /Pin to ProfileBookmark

YUI connection manager & multiple receiving divs

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);’>
[/code]

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
};
[/CODE]

Am I missing something obvious?

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @jon23d spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...