views:

808

answers:

6

This is not working. Firebug is not throwing any error though.

HTML:

<table>
       <tr><td>BookA</td><td><a href="javascript:deleteRow($(this));" class="red">Delete</a></td></tr>
       <tr><td>BookB</td><td><a href="javascript:deleteRow($(this));" class="red">Delete</a></td></tr>
        <tr><td>BookC</td><td><a href="javascript:deleteRow($(this));" class="red">Delete</a></td></tr>
        <tr><td>BookD</td><td><a href="javascript:deleteRow($(this));" class="red">Delete</a></td></tr>
</table>

Javascript:

function deleteRow(ref) {   
    $(ref).parent().parent().remove(); 
 }

If possible, I would like to use a solution with inline javascript

A: 

I believe you have a bug in your deleteRow function. Here's how it should be written:

function deleteRow(ref) {   
    ref.parent().parent().remove(); 
}

The ref that you are passing into deleteRow is already a jQuery object. You shouldn't use $(ref), just ref alone since ref is already a jQuery object.

Praveen Angyan
this does not work
Sergio del Amo
That's not a bug. It is unnecessary but not a bug. You can for example run $($(img)) with no errors
Nadia Alramli
+3  A: 

Try this:

// Bind all the td element a click event
$('table td.deleteRow').click(function(){
    $(this).parent().remove();
});

By the way, it'll remove the javascript from your html code. With this html code

<table>
    <tr>
     <td>BookA</td>
     <td class="red deleteRow">Delete</td>
    </tr>
    <tr>
     <td>BookB</td>
     <td class="red deleteRow">Delete</td>
    </tr>
    <tr>
     <td>BookC</td>
     <td class="red deleteRow">Delete</td>
    </tr>
    <tr>
     <td>BookD</td>
     <td class="red deleteRow">Delete</td>
    </tr>
</table>
Boris Guéry
The selector should be "a.red" to replicate what he's trying to do.
Paolo Bergantino
Yes, I was editing my post. But i use a new deleteRow class because i thought the red class was just here for the presentation... and maybe used elsewhere !
Boris Guéry
I prefer the deleteRow. I just use red to to use a css .red {color: red;}
Sergio del Amo
Fair enough; still, though, you should keep the link, as without it you lose the pointer hand (which you can get back with CSS, but why?) and lose semantic meaning.
Paolo Bergantino
If he links it with a database (and then a server-side script) or something then you are right !But if you want to respect semantic, i think in this case an input button should be the best since a click on it will trigger an action, and not forward the user somewhere..
Boris Guéry
+1  A: 

This won't work because $(this) isn't referring to the a-tag as you think (I think its referring to the window object or something)

Instead of using inline javascript in the href-attribute do this

Instead do this

<script type="text/javascript">
 $("table a").click( function() {
  $(this).parent().parent().remove();
 });
</script>
Kenny Eliasson
don't forget to wrap this in document.ready function
dalbaeb
You have syntax error with the first ".paren()" - missing a "t"
Jose Basilio
Thank you and fixed! =)
Kenny Eliasson
+8  A: 

First of all, inline JavaScript (href="javascript:x" or onclick="x") is generally bad. With inline JavaScript, you won't have access to the event object, and you can't really be sure what this references to.

jQuery (and almost every other JavaScript library/framework) has built-in event handling. So, your code would look like this with event handlers:

$('a.red').click(function(e) {
  e.preventDefault(); // don't follow the link
  $(this).closest('tr').remove(); // credits goes to MrKurt for use of closest()
});

And here's a demo: http://jsbin.com/okaxu

moff
+1 for the demo
RedFilter
Wow, a demo even! +1
dalbaeb
> you won't have access to the event object, and you can't really be sure what this references to. That it is why I am passing it as an argument
Sergio del Amo
I would use parents('tr') over parent().parent() - other than that, nice answer.
Paolo Bergantino
Yeah, but you're passing *this* as an argument – and in this case, this references to the window.
moff
Thanks everyone. @Paolo: You're definitely right – I've modified my post and the demo to use parents() now.
moff
.parents('tr') will grab all ancestor tr elements, which isn't right. I'd use .closest('tr').
MrKurt
@MrKurt: That's a fair point. I would hope no one does monstrosities like tables within tables, but closest would be the best one here on second thought.
Paolo Bergantino
Well, there are semantically valid reasons to have tables within tables. :)
MrKurt
Wow I learnt something today.
Omar Kooheji
This code doesn't work if you are generating the table rows with javascript (e.g. with an "Add row" button"), which is reasonably likely considering the functionality we are discussing is to "delete a row". In this case you would have to use inline javascript.
Daniel Alexiuc
+1  A: 

remove inline scripting

<script>
    $(function(){
        $('table td a').live('click', function(){
            $(this).parents('tr').remove();
            return false;
        });
    });
</script>
Dmitri Farkov
A: 

I would have to agree that inline javascript should be avoided, but if there is some other reason that it is necessary or beneficial to use it, here's how.

<table>
   <tr><td>BookA</td><td><a href='#' onclick="$(this).parents('tr').remove(); return false;" class="red">Delete</a></td></tr>
   <tr><td>BookB</td><td><a href='#' onclick="$(this).parents('tr').remove(); return false;" class="red">Delete</a></td></tr>
   <tr><td>BookC</td><td><a href='#' onclick="$(this).parents('tr').remove(); return false;" class="red">Delete</a></td></tr>
   <tr><td>BookD</td><td><a href='#' onclick="$(this).parents('tr').remove(); return false;" class="red">Delete</a></td></tr>
</table>