IntelliJ Java IDE
The moose likes HTML, CSS and JavaScript and the fly likes OnChange Event Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply Bookmark "OnChange Event " Watch "OnChange Event " New topic
Author

OnChange Event

Joe Brigs
Ranch Hand

Joined: Jun 06, 2011
Posts: 52

I have a text box that I only want to edit when user does a change. So I code an "onchange" event and do the edit. But if the user doesn't change anything and they hit submit button, I don't want to do the edit. Here's the problem. if user does a change to field the "onchange" event gets triggered and works properly. Except when they enter enter invalid data twice. The 1st time the onchange gets triggered , if theres bad data alert box tells them the error. But then they fix data but its invalid again. But the onchange does trigger again. I tried onBlur , but if user tabs or clicks and doesn't make a change, I don't want them to get error. Any suggestions. Thanks
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 339

If at all possible, could you include the relevant code (html, javascript)?
Joe Brigs
Ranch Hand

Joined: Jun 06, 2011
Posts: 52

The "Start Date" Field is intitally populated from a DB. If the user never changes this initial value I don't want to edit becuase the intital value would not pass these edits. Code is below. Thanks

Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15003
onchange just fires when the user changes the text. The onblur event fires when they leave the element.

Eric
Joe Brigs
Ranch Hand

Joined: Jun 06, 2011
Posts: 52

I tried onBlur , but if user tabs or clicks and doesn't make a change, I don't want them to get error. Example --> If intital value is 1997-01-01 ( comes from master record ) and they dont make a change that value is ok. ( They make a change to some other field ). But they change it to 1996-01-01 then error , expected. But if i use onblur and they tab to text box or put cursor there by mistake, thats not really a change but edits fire off and edit error gets hit. Hope that makes my problem clearer. Kind of screwy but thats the spec
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15003
Well you need to add in the logic to determine if it is from the server or if the user changed it.

Eric
Joe Brigs
Ranch Hand

Joined: Jun 06, 2011
Posts: 52

How about a hidden field where I will store the orginal DB server value and then check to see if it changed ?
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15003
or you can use the defaultValue property.
Joe Brigs
Ranch Hand

Joined: Jun 06, 2011
Posts: 52

Eric , that did the trick. I'm new to Javascript and I've only read the Head First JavaScript book which was a great start, but obvisouly I need someting more advanced at this point ( defaultValue property wasnt mentioned in the book along with other stuff I've wanted to do ) . Can you or anyone else recommend an advance Javascript book.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15003
Only thing that would probably mention stuff like that would be JavaScript the definite Guide, I doubt you would sit and read that whole thing.

Eric
 
 
subject: OnChange Event
 
Threads others viewed
JComboBox ActionEvent
How to calculate the time difference in javascript?
Dropdown
onblur, onchange, button clicked
problem with <h:selectOneMenu > element and the submittion of its value
Two Laptop Bag