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  |  Permalink  |  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 »
6,516 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 »
6,516 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?


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »