0

I am trying to make a button that when clicked, it will remove the row from the table. The problem is that I cannot use 'this' to pass an instance of itself into the function because it is wrapped in a 'a href' hyperlink.

<tr>
    <td>
        <a  href="#" 
            onclick="return removeContact(\''.$row['ContactMail'].'\', this);">
    </td>
</tr>

Any ideas on how to get this working?

1
  • Concerning your function, could you please add the code for removeContact? One error I see right away, is that you are not closing the a tag and don't have any content in it. Commented Feb 4, 2011 at 10:46

3 Answers 3

3

Use this.parentNode to get the TD or this.parentNode.parentNode to get the row.

Sign up to request clarification or add additional context in comments.

2 Comments

From there, if I have no tr id's <tr id="blah" then how do I use this row object to manipulate .display and .visibility?
you don't need an id to manipulate an element, you just need the element/object. this.parentNode.parentNode.style.visibility = false .
0

Or you can put some Id into TR tag and call it using the jQuery wrap like this:

$("#TrNNN").remove()

Comments

0

A nice way to do this is with JQuery:

$('a').click(function() {
  this.parentNode.parentNode.remove()
});

10 Comments

This effectively makes it so when any link is clicked, its parent table row is deleted.
This is why I hate jQuery. It's not the answer to everything. You're recommending a huge dependency to solve a single-line problem. Learn javascript first as use jQuery for complex tasks.
I'm not saying that it's the answer to everything. If you look at what he's also doing, creating a function to remove the record, he's basically reproducing an already existing JQuery function. The reason why this solution might work for him is that often times you're not dealing with a record. Usually multiple. This allows him the flexibility to either hit all links or if he gets a class into that selector, hone into the ones he has. As for using JQuery in general, I'm pretty well seasoned in Javascript. I just think recreating functionality that already exists is a waste of time.
You'd actually have to use $(this.parentNode.parentNode).remove();. The Plain Ol' JS(tm) would look something like a variation of this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode), though hopefully with objects properly stored in variables.
@Paul: that's correct, but it's not a jQuery object wrapping the element, so you're accessing native DOM properties/methods, for which remove() doesn't exist.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.