Using jQuery To Leverage The OnChange Method Of Inputs

Posted November 18, 2008 at 8:24 AM

Tags: Javascript / DHTML

The other day, I was working on an in-line-editing style datagrid and in order to minimize the datagrid's AJAX calls (required for saving), I wanted to only save records that have been altered in some way. At first, I started down the path of storing original values with the intent to compare them to input values on "blur" or on "focus" as needed. This got really complicated really fast.

Then, it dawned on me - text inputs, like select boxes, have an onChange method that fires when the text value of a given input changes. I think that I've completely forgotten about this method. In fact, I'm not sure if I've even ever used this method before in an application (outside of the select box).

Anyway, I was so excited at how easy this made flagging rows as dirty in my datagrid, I wanted to share it in case others had completely forgotten that it existed.

 Launch code in new window » Download code as text file »

  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html>
  • <head>
  • <title>jQuery Leveraged OnChange Method</title>
  •  
  • <style type="text/css">
  •  
  • input.dirty {
  • background-color: #660000 ;
  • color: #FFFFFF ;
  • }
  •  
  • </style>
  •  
  • <script type="text/javascript" src="jquery-1.2.6.min.js"></script>
  • <script type="text/javascript">
  •  
  • // When DOM loads, init the page.
  • $( InitPage );
  •  
  • // Init the page.
  • function InitPage(){
  • var jInput = $( ":input" );
  •  
  • // Bind the onchange event of the inputs to flag
  • // the inputs as being "dirty".
  • jInput.change(
  • function( objEvent ){
  • // Add dirtry flag to the input in
  • // question (whose value has changed).
  • $( this ).addClass( "dirty" );
  • }
  • );
  • }
  •  
  • </script>
  • </head>
  • <body>
  •  
  • <h1>
  • jQuery Leverages OnChange Method
  • </h1>
  •  
  • <form>
  •  
  • <p>
  • Data Item 1:
  • <input type="text" id="d1" value="" />
  • </p>
  •  
  • <p>
  • Data Item 2:
  • <input type="text" id="d2" value="" />
  • </p>
  •  
  • <p>
  • Data Item 3:
  • <input type="text" id="d3" value="" />
  • </p>
  •  
  • </form>
  •  
  • </body>
  • </html>

In this example, we are adding a "dirty" class to any input that get's changed. This class doesn't, in and of itself, do anything; but, it's not too difficult to imagine leveraging this dirty class (or other hidden flags) to alter the behavior of a page.

When I add some text to the inputs in the above example, my page looks like this:

 
 
 
 
 
 
jQuery Can Leverage The OnChange Method Of Text Inputs. 
 
 
 

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Nov 19, 2008 at 5:44 AM // reply »
1 Comments

Thank you for a really nice post!
Your idea sounds great, but I wonder how do you intend to treat the case when data was changed and then changed back to it's original value (without submission).
It will stay "dirty" in this case...


Nov 19, 2008 at 8:09 AM // reply »
7,572 Comments

@Vvvlad,

It's interesting, I actually thought of that use case. But then, I realized that that is an artificial concern. When we look at a text field and change the value, it becomes dirty - period. If someone then goes back and changes it again, pre-save, back to the original value, I think that it is a false construct that we should now consider that field to be no longer dirty. I know that might sound odd, but it's just something I feel.


Jul 6, 2009 at 10:47 AM // reply »
1 Comments

Thanks for the post, its a simple example but can be adapted to other more complex items. Much appreciated.


Sep 18, 2009 at 3:31 PM // reply »
1 Comments

Hi Ben,

Thanks for the input dirty, How can i expand the same to textarea.


Sep 18, 2009 at 3:33 PM // reply »
7,572 Comments

@Ravi,

This should work just the same for textareas as it does for inputs.


Oct 21, 2009 at 2:59 AM // reply »
1 Comments

Great script, but does it work with select and checkboxes?


Jan 27, 2010 at 11:55 AM // reply »
1 Comments

Nice Script! very useful, thank you.


Mar 19, 2010 at 12:00 PM // reply »
1 Comments

Thnx. FYI spelling error in your comment "// Add dirtry flag to the input in"


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 22, 2010 at 3:08 AM
Ask Ben: Selecting XML Attributes Given Other XML Attributes
Thanks for the response. I finally discovered that I was getting this error because I had cfsetting enablecfoutputonly="yes" in Application.cfc, and was neither setting it to false elsewhere nor brac ... read »
Mar 21, 2010 at 8:57 PM
The Bourne Ultimatum Starring Matt Damon And Julia Stiles
late to the party, but my observation is this: rewatch carefully for the platonic nature of the relationship between nicki and jason. she never flirts with him. he never comes on to her. they alway ... read »
Mar 21, 2010 at 7:40 PM
Is Simulating User-Input Events With jQuery Ever A Good Idea?
A couple of things. One you embed the initial state of of more-info in the CSS. IMHO, that behavior should be in jQuery: moreInfo.hide(); It shows that the behavior your toggling and closing is mor ... read »
Mar 21, 2010 at 3:59 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Elliott, according to Ben's experiment, serializeJSON() doesn't access the private data by default - it doesn't even access the getHair() method - so trying to clone a Girl.cfc via serializeJSON/des ... read »
Mar 21, 2010 at 3:49 PM
Ask Ben: Javascript String Replace Method
I'm confused a bit by what you are asking, but if had this sentence: The color, red, is in the style statement; style: red;. and wanted to remove all or change all of the commas, colons, and semi-c ... read »
Mar 21, 2010 at 3:13 PM
Ask Ben: Javascript String Replace Method
I am trying to make a java program to count the number of times that these punctuation marks occur in a body of text: , : ; . ! - ' " ? / \ I am using this piece to ferret out the commas: numcommas ... read »
Mar 21, 2010 at 11:13 AM
A New Wrist Pain
@chiropractor suwanee, Spoken like someone trying to sell something. Other than for minor, temporary relief from some back pain, chiropractic treatment is nothing but placebo effect and quackery. ... read »
Mar 21, 2010 at 6:32 AM
ColdFusion CFPOP - My First Look
Apologies... The field name in the db for C. is "BounceCode" It stores the code / message which is returned in the email. Sorry for the confusion. ... read »