/    Sign up×
Community /Pin to ProfileBookmark

Array Problem adding onkeyup function

Hi, I’m trying to do the following in the window.onload

for (s in myfields) {
myfields[s].onkeyup = myfunction(myfields[s]);
}

but only the last field is getting the function…

Thanks.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@cyberowlauthorAug 29.2007 — lol, now not even the last one...
Copy linkTweet thisAlerts:
@Mr_MooAug 29.2007 — Try this:
for (s in myfields) {
myfields[s].onkeyup = function() { myfunction(myfields[s]); };
}
Copy linkTweet thisAlerts:
@KorAug 29.2007 — <i>
</i>myfields[s].onkeyup = [B]function(){[/B]myfunction([B]this[/B])[B]}[/B];
Copy linkTweet thisAlerts:
@DokAug 29.2007 — Depends on which scope you're using

If you're doing
[CODE]window.onload =
function() {
// Get myfields somehow first
for (s in myfields)
myfields[s].onkeyup = myfunction(myfields[s]);
};[/CODE]

Then you're assigning the return value of myfunction as the onkeyup event handler which is probably not what you want

If you're doing
[CODE]window.onload =
function() {
// Get myfields somehow first
for (s in myfields)
myfields[s].onkeyup = function() { myfunction(myfields[s]); };
};[/CODE]

then you're creating closures and and onkeyup will always be executed with s having the last value of the for..in enumerating. Mr. Moo - please correct me here if I'm wrong ?

If you're doing
[CODE]window.onload =
function() {
// Get myfields somehow first
for (s in myfields)
myfields[s].onkeyup = function(){myfunction(this)}; };
};[/CODE]

then 'this' will refer to the window/global object everytime the onkeyup event is fired and you're still creating closures.

I'm guessing you want to do something like this
[CODE]function fieldOnKeyUp() {
// Do whatever. 'this' is refering to the HTML element which fired the event
// Alert the value of the field
alert(this.value);
}

window.onload =
function() {
// Get myfields somehow first
for (s in myfields)
myfields[s].onkeyup = fieldOnKeyUp;
};[/CODE]
Copy linkTweet thisAlerts:
@Mr_MooAug 29.2007 — Dok, you are right (and hence my original post is incorrect; I need to drink coffee before I post on the Internet). Kor's solution (and yours) is correct.
Copy linkTweet thisAlerts:
@cyberowlauthorAug 29.2007 — Thanks for the answers everybody. Works fine now with this.
×

Success!

Help @cyberowl 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.16,
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,
)...