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,371 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.


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


Ravi
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,371 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 7, 2009 at 5:53 PM
Ask Ben: Javascript String Replace Method
You can find here an advanced function that prepared with javascript replace function. This can make the first letters of words, sentences, lines and whatever you define automatically: http://www.m ... read »
Andrew Neely
Nov 7, 2009 at 4:56 PM
A Moment That Touched Me - The Fountainhead
Ben, Glad you enjoyed the podcast. Yeah, the Tank Riot guys can get really chatty during the episodes, but that's part of the charm of it for me. They've covered everything from Nichola Tesla to Cha ... read »
Nov 7, 2009 at 4:43 PM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
Is it possible to make some more MenĂ¼`s ? ... read »
Jill
Nov 7, 2009 at 11:40 AM
How To Unformat Your Code (Like A Pro)
Derek, I think you might be right - sweet! Thanks for the link :) ... read »
Nov 7, 2009 at 11:25 AM
How To Unformat Your Code (Like A Pro)
I think it would be way easier to just use this http://www.logichammer.com/html-formatter/ He just released v3 and it rocks. ... read »
Jill
Nov 7, 2009 at 7:58 AM
How To Unformat Your Code (Like A Pro)
LMAO - this was pretty funny! I have to admit - I also love to reformat code so I can read it. My boss used to tell me to leave my OCD at home. Now I don't feel so bad after reading everyone else' ... read »
Nov 6, 2009 at 10:10 PM
How To Unformat Your Code (Like A Pro)
The timing of this post is just uncanny. I spent the last 15-20 minutes manually un-formatting my "Ben Nadel" style code within a CFC of mine. I was really digging the readability a few weeks ago, bu ... read »
Roe
Nov 6, 2009 at 5:11 PM
Passing Arrays By Reference In ColdFusion - SWEEET!
ArraySort also reorders the results of these java obj's ... read »