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?
There is no OnClick server event for td.
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.
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';
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With