Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<tr> onClick not working

I want to turn my table rows into links using JS. I have it looking like this:

<tr onClick='javascript:window.location.href='url';'>

However, when I try to click, it doesn't go the page as I want. In fact, clicking seems to have no action.

Any help?

Edit:

As for the quotes, I forgot to mention that I'm echoing this with PHP. Here's my updated code:

echo "<tr onClick='window.location.href='url?id=" . $var . "';'></tr>";

Should I be doing some sort of escaping like /" in this case?

like image 637
AKor Avatar asked Mar 16 '11 00:03

AKor


People also ask

Can we use OnClick in TD tag?

There is no OnClick server event for td.

Can we use OnClick on Div?

We can bind a JavaScript function to a div using the onclick event handler in the HTML or attaching the event handler in JavaScript. Let us refer to the following code in which we attach the event handler to a div element. The div element does not accept any click events by default.


1 Answers

First of all, no javascript: in event handlers - they contain JavaScript code, not an URL. It just works because javascript: is a label in this case and thus not a syntax error. Besides that, any editor with proper syntax highlighting would have shown you that you are breaking the quoting as you are using single quotes for the HTML attribute and inside the attribute.

Here's the fixed code:

<tr onclick="window.location.href = 'url';">

Besides that, inline event handlers are dirty. Better attach them nicely using jQuery:

$('tr').click(function() {
    location.href = 'url';
});
like image 58
ThiefMaster Avatar answered Sep 27 '22 20:09

ThiefMaster