Monday, 9 September 2013

ON binding too much recursion

ON binding too much recursion

I am trying to bind a handler on a UL to detect when a div is clicked
inside LI and then trigger a hyperlink click event within the LI.
Note: I only want clicking .col2 to trigger the event.
I am getting too much recursion error because the click event of the
hyperlink is trigger the click event on the UL
I have tried stopPropagation but it doesnt seem to work, any ideas?
$('#ul_search').on('click', 'div.col2', MyClickHandler);
function MyClickHandler(e) {
e.stopPropagation();
var control = $(this);
control.find('a').click();
e.stopPropagation();
}
<ul id="ul_search">
<li>
<div class="col1">
Col1
</div>
<div class="col2">
Go to
<a href="http://www.google.com">Google</a>
by clicking here
</div>
</li>
<li>
<div class="col1">
Col1
</div>
<div class="col2">
Go to
<a href="http://www.yahoo.com">Yahoo</a>
by clicking here
</div>
</li>
</ul>

No comments:

Post a Comment