Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using onClick in a table row that also has links in it

I'm using onClick in table rows as follows:

<tr onmouseover="this.style.cursor='pointer'" onclick="http://www.whatever.com/index.php?var={$foo1}">

I also have some needs to put a link in certain areas in this table row, i.e.

<td>Stuff Here</td>
<td>Stuff Here</td>
<td>Stuff Here</td>
<td><a href="specialLink.php?var={$foo2}">Here</a></td>
<td>Stuff Here</td>

The problem is, the "inner link" (specialLink.php) can't be clicked, because the row link takes precedence. Do I have any options here?

Thanks

like image 781
Shackrock Avatar asked Dec 23 '11 21:12

Shackrock


People also ask

How do you make a whole row in a table clickable as a link?

To make the entire row as clickable in this case, one can use a link <a> tag to wrap its content.

Does Onclick work with links?

Using onclick Event: The onclick event attribute works when the user click on the button. When mouse clicked on the button then the button acts like a link and redirect page into the given location.

How do I make a row in a table selectable?

To make the full row selectable, you should add the link to the <tr> instead of to individual table cells. Since you're not allowed to place an <a> tag around a table row, you'll have to think a little bit outside the box to solve this puzzle.

How do you make a row clickable in JavaScript?

If you add "display: block" CSS style tag to the anchor objects in the cells that you want to be clickable it will make the entire cell (minus any padding) act like a button. The cursor is displayed correctly and it previews the link destination in the status bar.


2 Answers

You can use stopImmediatePropagation() to bypass consecutive/incoming events.

Short example:

<tr onclick="alert('tr event')">
    <td>tr event</td>
    <td onclick="alert('td');event.stopImmediatePropagation();">NO tr event</td>
</tr>

Test this on jsfiddle: http://jsfiddle.net/g8Anh/6/

like image 194
Marcelo Amorim Avatar answered Oct 06 '22 22:10

Marcelo Amorim


I 'll do that :

<script type="text/javascript">
var link=true;
</script>

<tr onmouseover="this.style.cursor='pointer'" onclick="if (link) window.location ='http://www.whatever.com'">

<td>Stuff Here</td>
<td>Stuff Here</td>
<td>Stuff Here</td>
<td onmouseover="link=false;" onmouseout="link=true;"><a href="specialLink.php">Here</a></td>
<td>Stuff Here</td>
like image 22
Kevin Avatar answered Oct 06 '22 20:10

Kevin