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?
 
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.
0
 
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"
0
 
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>
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
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.
0
 
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.
0
 
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.


0
 
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
0
 
NushiCommented:
Jester_48
::-))

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

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

Nushi.
0
 
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.

0
 
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
0
 
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.

0
 
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.
0
 
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>

 
0
 
DidierDAuthor Commented:
The first (onclick) should be (onchange) Sorry
0
 
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()" >

0
 
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.
0
 
DidierDAuthor Commented:
Thanks Nushi,

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

Thanks everybody else for trying to help me.

Greetz,
Didier
0
 
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.

0
 
NushiCommented:
:-))
Thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.