views:

33

answers:

3

In my HTML code, I've got a certain cell whose value should be cleared if a link button is pressed. I'd like to achieve this using jQuery and here's how I tried to do it but with no results:

var $delActionDate = $('#delete_Event');

        $delActionDate.live('click', function() {

            var myRow = $delActionDate.parent().parent();
            myRow.find('#dateCell').html() = "";

Any ideas on how to do this?

+2  A: 

You want, myRow.find('#dateCell').html("");

Luca Matteis
That's exactly what I want. The only problem here is, I've got multiple rows, no matter what button I click it's always the first row's cell value being emptied. How can I point it to the cell value of the button being pressed?
Hallaghan
@Hallaghan - Are you using an ID multiple times? That's invalid HTML that will give all sorts of issues :)
Nick Craver
Indeed, I'm doing it the wrong way. But I think your approach already cleared my mind in some aspects. I'm going to give it a try and change my code.
Hallaghan
+1  A: 

As Luca said, to update the html content you use html("") rather than html()="".

In additions, assuming your parent() stuff is correct based on your HTML, the change you need to make so that the selected row is based on which button is pressed should be: change

var myRow = $delActionDate.parent().parent();

to

var myRow = $(this).parent().parent();
JacobM
Remember to create a jquery object from this if you want to call the .parent() method.var myRow = $(this).parent().parent();
Michal
Your solution worked very well, thanks JacobM. Except that you should change this to $(this) otherwise you cannot call .parent()I'm thankful to everyone for the tips!
Hallaghan
Whoops -- good point. Edited to fix.
JacobM
+1  A: 

You should use classes instead of IDs since there are multiple elements, then use .closest() and .find() to get where you want, like this:

$('.delete_Event').live('click', function() {
  $(this).closest('tr').find('.dateCell').empty();
});

This means changing your elements to class="delete_Eevent" and class="dateCell" instead of id="delete_Event" and id="dateCell". .closest('tr') to climb up to the <tr>, not matter how deep, then does a .find() to go down to the date cell. Then we're using .empty() to clear the elements.

If you have any data or event handlers this is a better approach than .html(""), because it won't leak the memory stored in $.cache, which .empty() cleans up.

Nick Craver