/    Sign up×
Community /Pin to ProfileBookmark

how to handle multiple javascript events

Hi,
i have a page with an input with an onchange event that validates the value entered, it also has button that has an onclick event.
when a user puts text in the input and then pushes the button immediately, the onchange is called but the onclick is lost.

can someone help me?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@ChikaraOct 10.2007 — This is to vague without examples. Please show the code you have now and explain exactly what you want to do.

Are you trying to validate a forum before submission?
Copy linkTweet thisAlerts:
@ran_incauthorOct 10.2007 — Ok, its pretty simple.

What i want to do is that after the user enters info to the input and then pushes the button, the onchange will run and then the onclick will run.

The code is very simple:

<html>

<body>

<input onchange="alert('change')">

<input type="button" onclick=('click')">

</body>

</html>
Copy linkTweet thisAlerts:
@ChikaraOct 10.2007 — They will both work but onchange works first. Basically this is how things are done.

As you type things into the text box onchange isn't really looking as focus is still on that text box. When you click the button, the text box is blurred and onchange is called. Onclick from the button never gets a chance to fire.

To better understand the problem do this.

Type in some stuff in the text box then click someone else on the page. The onchange event will fire. AFTER you do that click on the button, then the onclick will fire.

I have ran into this problem a few times but alas, I haven't found a clean way around it. If you are trying to validate that text box, just save the validate call for the button. Or you could try using Keydown or Keyup events on that textbox, this will basically validate for each keystroke that is made.

I hope that helps.
Copy linkTweet thisAlerts:
@ran_incauthorOct 10.2007 — Hi, thanks for replying...

I already knew the stuff you wrote, i need a good work around....

BTW in firefox when you do the same scenario, the onclick fires first and then the onchange(no event is lost, tough the order is wierd).
Copy linkTweet thisAlerts:
@ChikaraOct 10.2007 — What exactly are you trying to accomplish with the onchange event? Are you trying to validate input? If it is input, how are you trying to validate it? As in, only number, only letter, only e-mail addresses?
Copy linkTweet thisAlerts:
@ran_incauthorOct 10.2007 — Actually its more complex than that.

The onchange goes to the server where the value is processed and returns to the client(it doesnt really matter what happens in the server, the thing is it has to be onchage or onblur).

To validate if its a number or something like that is easy with onkeyup.
×

Success!

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