views:

35

answers:

1

I just can't work this one out. I have a table with a column of checkboxes. After an Ajax update, I want to update a specific cell in the rows with checked checkedboxes. The jQuery Ajax call I'm using is:

$.ajax({
        type: 'POST',
        url: 'ProcessDate',
        data: params,
        error: function(xhr, ajaxOptions, thrownError) {
               alert(xhr.statusText);
          },

         success: function(html) {
                  closeDialog();
                  $('#results tbody tr').each(function(){
                         $("#CaseID:checked").parent().siblings("td.DateUpdated").html('CHANGE'); 
            });
        },
        cache: false
     });

And the HTML is:

<table id="results">
  <tr>
     <td><input type="checkbox" id="CaseID"></td>
     <td id="DateUpdated"></td>
  </tr>
  <tr>
     <td><input type="checkbox" id="CaseID"></td>
     <td id="DateUpdated"></td>
  </tr>
  <tr>
     <td><input type="checkbox" id="CaseID"></td>
     <td id="DateUpdated"></td>
  </tr>

The Ajax call succeeds but my table update doesn't happen.

A: 

First, use classes since Ids must be unique, like this:

<tr>
  <td><input type="checkbox" class="CaseID"></td>
  <td class="DateUpdated"></td>
</tr>

Then use class selectors, like this:

$.ajax({
  type: 'POST',
  url: 'ProcessDate',
  data: params,
  error: function(xhr, ajaxOptions, thrownError) {
    alert(xhr.statusText);
  },
  success: function(html) {
    closeDialog();
    $(".CaseID:checked").parent().siblings("td.DateUpdated").html('CHANGE'); 
  },
  cache: false
});

There's no need for a .each() loop, at least not with your example code...the class selector will get all the elements you want, regardless of the row.

Nick Craver
OK - IDs changed to classes. each() loop removed. Still no change in the html.
Wendy R
Could the parents().siblings syntax be incorrect? Am I pointing to the right area? Do I need the parents() selector?
Wendy R
@Wendy - Are you sure your callback is successful and the code is running? It works, you can test here: http://jsfiddle.net/nick_craver/cBLQL/ It sounds like something *else* is blowing up before it gets there, are there any errors in the console?
Nick Craver
Callback is successful. No errors in the console. I'll keep looking for whatever it is that is blowing up. Thanks for the help - at least I know this syntax will work.
Wendy R
Doh! My version here is a bit more complex than the example I gave you. The <td> I'm looking to change is further down the DOM. The code above is correct for my example. For my version, I had to look further, so its "$(".CaseID:checked").parent().siblings.find("td.DateUpdated").html('CHANGE');
Wendy R
@Wendy - Ah, yes if the HTML's different the answer is as well :)
Nick Craver