4

I have the following code:

Html:

<tr>
    <td onclick="ThingClicked()">click me</td>
</tr>

JS:

function ThingClicked(event) {
    var row = event.currentTarget.parent("tr");
    callAnotherDOMManipulatingFunction(row);
}

For some reason, event.currentTarget is undefined. I tried 'this' but that was undefined aswell. What's going on? How do I get the row that this td is contained in?

4 Answers 4

5

Well, since you included a jQuery tag (and included a link to jQuery docs), I'll show you how that way.

EDIT:

jQuery live() docs

jQuery clone() docs

EDIT: Added jQuery's ready() function. You will always place your code inside as shown.

$('document').ready(function() {
    $('td').click(function() {
        var row = $(this).closest('tr');
        callAnotherDOMManipulatingFunction(row);
    });
});

Of course, this will attach a click event to every td tag, but you get the idea.

EDIT:

Further explanation. When using jQuery, you typically will assign events in your javascript, not in HTML. Assigning to 'td' will give the click event to every 'td' tag on the page, so it may be that you will instead give the 'td' a class and use that to attach the event, like:

$('.myFreakingAwesomeClass').click(function() {
    ...
});

You won't often need to reference 'event', but it does come in handy at times.

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

6 Comments

+1 I didn't notice the jQuery tag. This is surely the way to go (unobtrusively)
I apologize for not making it more clear that jQuery was an option. Thanks for realizing this. I have one follow up question though... I'd like to give my function a name and define it separately and then attach it. Is that possible this way? I thought I tried this and $(this) didn't work at that point.
The main reason for this is that I'm dynamically adding these td's. If I do $('td').click( fn...) or something similar in the document).ready function, these new ones wont get the onclick.
Yes, you can put it in a separate function and assign that to the click event. But $(this) should be working. Is your code in jQuery's ready() function? I'll update my example with that.
jQuery has it covered for dynamically added events as well. You either use live() for elements created in javascript, or if you are cloning an existing element, simply use $('.myElementClass').clone(true) to clone the element along with its event handlers. (If you omit 'true' the event handlers won't be cloned.)
|
1

Try this:

<tr>
  <td onclick="javascript:ThingClicked(this);">click me</td>
</tr>

function ThingClicked(td) {
    callAnotherDOMManipulatingFunction(td.parentNode);
}

2 Comments

Don't put javascript: at the start of event handler attributes. It's not necessary and only doesn't cause a syntax error by coincidence.
@Tim - My bigger error was being too tired to notice the jQuery tag on the question, doh!
0

Change your code like this:

Html:

<tr>
    <td onclick="ThingClicked(this)">click me</td>
</tr>

JS:

function ThingClicked(element) {
    callAnotherDOMManipulatingFunction(element);
}

Comments

0

Try this:

<tr>
    <td onclick="ThingClicked(event)">click me</td>
</tr>

with the input parameter of your handler function specially called event and nothing else.

Comments

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.