views:

184

answers:

2

I have a table which has one row and only one cell will be editable. I have accomplished this with the following code.

$("td#effEndDate").click(function() {
            if (!$(this).hasClass("edit")) {
                var value = jQuery.trim($(this).html());
                $(this).html("<input id=\"txtEdit\" type=\"text\" value=\"" + value + "\" />");
                $(this).addClass("edit");
                $("#txtEdit").focus();
            }
        });

Now this is the part where i'm stuck.

After the field is updated a save button must be clicked to call the proper .ajax method to update the database. But how can I compare the previous value to the current value on a button press? Since i'm not using the onblur property where I could have saved the old value and passed it to the update function.

A: 

I would store the original value somewhere in a variable, and compare the two in the submission function before sending the .ajax call.

Voyagerfan5761
Thanks for your quick response. The original value is already being saved in the third line of the code. var value = jQuery.trim($(this).html());But this value is not visible to other functions. How could I save the value so it would be visible to other functions?
Randall Kwiatkowski
A: 

There are two possibilities.

  • Pass the variable around in between functions
  • Make the variable global

if you want the variable global do not use the "var" keyword

Change:

var value = jQuery.trim($(this).html()); 

To this:

value = jQuery.trim($(this).html()); 

Edit

If the click function is getting hit more then once before a page refresh and assuming you want to keep a copy of the original table rows you can try this. Save a copy of the original table in a variable then you can query the original table for the html using the ID number. Here is a quick mock

first store the table in a variable upon the page loading. This will save an original copy of the table

 //get a copy of the table
 var GetCopyofOriginalTable = function() {
      var TableToBeCopied = $('the appropriate selector');
      CopyOfTable = JQuery.extend(true, {}, TableToBeCopied);  //Notice no var, its global
 }

 //Now store the variale
 GetCopyofOriginalTable();

 var FindTableRowByID = function(trID) {
      return $('table:has(tr#' + trID));
 }

Now you can loop through the new table test its value agaisnt the old table. This methed make use alot of memory depending on how big the table is.

Luke101
Thanks, yes that does work. But if I wanted to make more than one cell editable the .click function will be called more than once thus wiping out the previous value. Is there a way to save the whole <tr> and store it into an object or something and then compare it to the new values?
Randall Kwiatkowski
Hello, I have edited my answer to reflect your comment.
Luke101