/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] onchange Doesn’t Trigger Function Call

THIS WORKS:

<html><head><script type=”text/javascript”>
function recalc() {
alert (‘Entering recalc().’);
}
</script></head><body>
<form><input name=”tf_addr” id=”tf_addr” type=”text”></form>
<script type=”text/javascript”>recalc()</script>
</body></html>

THIS WORKS:

<html><head><script type=”text/javascript”>
</script></head><body>
<form><input name=”tf_addr” id=”tf_addr” type=”text” onchange=”alert(‘onchange works fine’)”></form>
</body></html>

BUT THIS DOESN’T WORK:

<html><head><script type=”text/javascript”>
function recalc() {
alert (‘Entering recalc().’);
}
</script></head><body>
<form><input name=”tf_addr” id=”tf_addr” type=”text” onchange=”recalc()”></form>
</body></html>

The first example works, which shows that the recalc function can be called. The second example works, which shows that onchange will trigger an alert box. BUT, the third example doesn’t work. Why can’t I trigger a call to the recalc function from the onchange event?

The above 3 code postings are the entire programs. Nothing has been left out or modified.

I am using IE 8.0.6001.18702.

I am sure the solution is simple enough, but I’m just not seeing it. Can anyone out there help me?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Jul 14.2010 — [url=http://msdn.microsoft.com/en-us/library/ms536685&#37;28VS.85%29.aspx]recalc[/url] is the problem. Change the name and it will work.
Copy linkTweet thisAlerts:
@WarrenGaebelauthorJul 14.2010 — Many thanks, Declan. Your solution works fine.

If I may bother you one more time: Why does this solve the problem?
Copy linkTweet thisAlerts:
@Declan1991Jul 14.2010 — For the same reason that you don't make functions called alert. recalc seems to be a propitiatory function of IE that does some re-calculation, what exactly I don't know.

The other one you might run into is that all ids are global variables in IE. So don't name a function the same as an id on the page.
×

Success!

Help @WarrenGaebel 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 5.7,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...