I have the following HTML:
<table class="products">
<tr>
<td>Product description probably goes here</td>
<td><a href="#">more information</a></td>
</tr>
</table>
To make it a bit sexier for Javascript-enabled browsers I've added this jQuery (1.3.2):
$(document).ready(function(){
$('table.products tr').click(function(){
$(this).find('a').click();
return false;
});
});
But I think it gets into an infinite loop or something. Safari shows this:
RangeError: Maximum call stack size exceeded. jquery.min.js:12
Can anyone please offer a workaround or better way?
How to make this a tag working with href and onClick? The default behavior of the <a> tag's onclick and href properties are to execute the onclick , then follow the href as long as the onclick doesn't return false , canceling the event (or the event hasn't been prevented).
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.
The HTMLElement. click() method simulates a mouse click on an element. When click() is used with supported elements (such as an <input> ), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.
First, you have to block href call URL if don't want to open the tab/window or other operations. You can call a function in 2 ways using the href or onclick attribute in the HTML tag.
The problem here is that a lies within the tr. Ergo, when the a click() method is triggered, the event bubbles to the tr containing it, creating the recursive loop you mentioned. Rather than call .find('a').click()
, have both a
and tr
use the same click method.
$('tr, a').click(function(){
//dostuff
return false;
});
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