views:

171

answers:

3

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?

+1  A: 

Try this:

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

function ThingClicked(td) {
    callAnotherDOMManipulatingFunction(td.parentNode);
}
Nick Craver
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 Down
@Tim - My bigger error was being too tired to notice the jQuery tag on the question, doh!
Nick Craver
A: 

Change your code like this:

Html:

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

JS:

function ThingClicked(element) {
    callAnotherDOMManipulatingFunction(element);
}
Pablo Fernandez
+4  A: 

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.

patrick dw
+1 I didn't notice the jQuery tag. This is surely the way to go (unobtrusively)
Pablo Fernandez
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.
sdr
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.
sdr
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.
patrick dw
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.)
patrick dw
Wow. jQuery is amazing! :) That's really cool. Thank you very much for all your help.
sdr