onclick does not work after onchange

This is probably easy, but i can not figure out how to do it. If i change the input field in the example and then click the button, only the onchange is executed. Why? How can i solve this? I want the onchange first executed and then the onclick.

<html>
<head>
      <title>Untitled</title>
<script>
  function gotoOK(){
    alert("OK");
  }
 
  function doUpdate(){
      alert("update");
  }
</script>      
</head>

<body>

<input type=button name=okButton  value="OK"  onclick="gotoOK()" >
<input type=text name=test  value=""  onchange="doUpdate()">

</body>
</html>
LVL 2
DidierDAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jaysolomonCommented:
onblur
<input type=text name=test  value=""  onblur="doUpdate()">


also the reason its happening is because the field has to loose focus before its considered "onchange"
James RodgersWeb Applications DeveloperCommented:
this onchange

<input type=text name=test  value=""  onchange="doUpdate()">


takes the focus away from the button, halting the onclick event

try this instead

<html>
<head>
     <title>Untitled</title>
<script>
  function gotoOK(){
    alert("OK");
  }
 
  function doUpdate(){
     alert("update");
       return true;
  }
</script>    
</head>

<body>

<input type=button name=okButton  value="OK"  onclick="doUpdate();gotoOK()" >
<input type=text name=txtjkltest  value="">

</body>
</html>
NushiCommented:
the onchange is called when you lose focus from the object,.

if you want both of the fucntion simply write them toghter:

<input type=text name=test  value=""  onchange="doUpdate();gotoOK()">


Nushi.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

NushiCommented:
everyone is typing answers at the same time.

DidierD
all of the above is correct.
you can accept all answers and split the points.
read my next comment where you will find more in formation in few minutes
(it takes time to type...)

:-))
Nushi.
NushiCommented:
whenever your text filed lose focus (by clicking the buton you set the focus to it)
it calls the onchange event so you never actually reach the gotoOK since you are never
able to click on the button:

text -> (go to button) -> lose focus -> call onchange -> open alert -> (get focus in order to close it)

from the above flow you can see that you never actually click on the button so
you didnt invoke the gotoOK function.

please feel free to ask if anything is still doesnt clear to u
after this explanation.

:-))
Nushi.


James RodgersWeb Applications DeveloperCommented:
>>everyone is typing answers at the same time.
>>(it takes time to type...)

and the way i type im lucky to get any answers in
NushiCommented:
Jester_48
::-))

its much faster with faces....
NushiCommented:
Jes

im thinking of using a speach recognition software that will help me in this site...

Nushi.
NushiCommented:
DidierD

do you still have any questions?
if you do feel free to ask if you find my explanation doesnt statisfing
enought for your needs.

Nushi.

DidierDAuthor Commented:
Thanks for these fast replies. But i'm afraid they don't solve my problem.
On my real page i have more inputfields, with all a different onchange. I simplified my example a little too much. So onchange="doUpdate();gotoOK()" is not an option for me.
I tried the onblur, but only the alert with update appeared.

Didier
NushiCommented:
so please explain what is it that you wish to do?

to call several method?
so use one which calls them all

function a(){
 b();
 c();
etc...
}

or maybe you want to capture the context of the filed?


please explain what you want to achive so i can help you.


Nushi.

DidierDAuthor Commented:
I'm using JSP/Struts.

The page that is generate contains

text fields with associated hidden fields. When the text field changed the hidden field get status updated (onclick). When i press OK(onclick) the page is submitted. I can then see in my struts action which fields have a hidden field with value updated. So i only have to send the fields xith status updated to the DB.
James RodgersWeb Applications DeveloperCommented:
ok try this

<html>
<head>
     <title>Untitled</title>
<script>
  function gotoOK(){
    alert("OK");
  }
 
  function doUpdate(){
     alert("update");
      gotoOK();
  }
</script>    
</head>

<body>

<input type=button name=okButton  value="OK"  onclick="doUpdate();gotoOK()" >
<input type=text name=txtjkltest  value="">

</body>
</html>

 
DidierDAuthor Commented:
The first (onclick) should be (onchange) Sorry
James RodgersWeb Applications DeveloperCommented:
doh...

and change this

input type=button name=okButton  value="OK"  onclick="doUpdate();gotoOK()" >

to

input type=button name=okButton  value="OK"  onclick="doUpdate()" >

NushiCommented:
so why no use onkeyUp to update the hidden fields name?
using keyup whenever user typyes something it will update whet ever you wish.

<input type=text name=txtjkltest  value="" onkeyUp="document.getElementById('hidden-field.id').value= this.value">

Nushi.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DidierDAuthor Commented:
Thanks Jester_48

But i want to do first all my updates and then the submit.

I could keep the old values in hidden fields and then compare them in the doUpdate(). or just send everything then to the struts action and compare them there.If i do it like this your solution works. But was hoping i could do it without much changes to my code.
DidierDAuthor Commented:
Thanks Nushi,

I knew this should be simple :)) Your solution works.

Thanks everybody else for trying to help me.

Greetz,
Didier
NushiCommented:
>>Thanks Nushi,
>> I knew this should be simple :)) Your solution works.

even after you close this question im here to help you if you need something else regarding this.
P.S.

i hope its an A 9just for my record).
Thank you.
Nushi.

NushiCommented:
:-))
Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.