2

I have nested event listeners. Exactly they are

<tr onclick="function1();">
<td>....</td>
<td onclick="function2();">....</td>
</tr>

When Second Cell is clicked, only function2() should be called and not function1(). Is there any way to do this?

Pranjal
  • 796
  • 1
  • 8
  • 24
  • if you are using a function on td, i think there is no use of tr cause tr is departed by one or many table datas(tds) – polin Oct 20 '12 at 08:41

2 Answers2

1

Pass event object in the function and call stopPropagation to stop event propogation.

Live Demo

<tr onclick="return function1(e);">
  <td>First Td....</td>
 <td onclick="return function2();">second td....</td>
</tr>

function function2(e)
{
  //your statements.
   alert("function2");
   e.stopPropagation();
}
Adil
  • 146,340
  • 25
  • 209
  • 204
1

The event can be stopped using event.stopPropagation

function(e) {
   var event = e || window.event;
   event.stopPropagation();
}

See the answer here for more discussion: How to stop event propagation with inline onclick attribute?

Community
  • 1
  • 1
benedict_w
  • 3,543
  • 1
  • 32
  • 49