views:

70

answers:

5

I've been trying to develop a way to make rows of an ASP.NET GridView (or table in general) clickable, whilst still maintaining all the usual actions you'd associate with links - correct right-click context menu, middle-clickability to open in a new tab, etc.

My solution uses jQuery to find <a> tags within each row and expand that <a> tag to the size of the row, thus making it appear that the row is clickable. It all works fine in Firefox and Chrome, but IE gives priority to the the text in the table rather than the <a> tag, so you get a text selector cursor and can't click the link when you hover over the text. This simple example shows what i mean:

<style type="text/css">
.link {position: absolute; z-index: 100; top: 200px; border: 1px solid pink; width: 150px; height: 150px;}
.content {position: absolute; z-index: 0; top: 200px; border: 1px solid red; width: 150px; height: 150px;}
</style>

<a href="#" class="link">link here</a>
<div class="content">
    You can't click the link when you hover over this text in IE    
</div>

This could be by design for accessibility, but if that was the case I'd expect other browsers to follow suit. Can anyone offer any suggestions as to how to avoid the issue?

A: 

why not just make the <a> display:block and add the other div inside of it?

Jonathan Fingland
I'm trying to keep it syntactically valid - <div> can't live in <a>, just like <tr> can't live in <a>. If that was the case I could just do <a href=""><tr> on my table and have done with it! More's the pity...
Town
then change the div to a span and make both to display:block. is the content inside really semantically different as a div than as a span
Jonathan Fingland
Good plan for the simple example scenario, not so feasible for making my GridView/table rows clickable! Cheers though :)
Town
+1  A: 

Probably one of the issues where IE creates a new z-index stacking context where it shouldn't. Either you've got a positioned parent without a z-index (known IE bug), or maybe IE just does that for table cells anyway, since that introduces a new offsetParent.

The normal approach would be to put a link in every cell (containing only a nbsp if it's empty) and use ‘display: block’ to make the link fill the width of the cell.

bobince
Cheers bob - i think i may well have to go with the <td> approach. It's so frustrating to get this close and have to go back to the start! Not the first time, or the last i'm sure :)
Town
A: 

Alternative approach: instead of making the link the size of the row, why not make the entire row clickable?

$('tr').click(function(){
    $(this).find('a.link').click();
});

You may want to use the css cursor property to make the row appear clickable as well.

cpharmston
I thought of suggesting that, but he made a specific point regarding right-clicking, which would not work simply because the area had a click event handler. At least, I don't think it would.
Anthony
Cheers, but that does away with all the expected functionality you get with a link (no right click menu, no middle-click open in new tab etc) which i'd like to avoid (ideally!)
Town
+1  A: 

Have you tried setting the padding of the a to fill the table row?

The tricky part with that is you'd have to do the math so that it didn't exceed the row. So something like:

$("a").css("padding-top", function() {
         $(this).closest("tr").height - $(this).height;
       });

and so forth, based on the position of the a to the table row.

My thinking is that maybe IE sees overlapping elements (as in positioned on top of one another) as a possible conflict and thus makes a decision to try to accommodate both ("well, the link is yay big, but the text outside of it really isn't a link, so let's treat it like normal text") etc, but that elements that overlap purely for style (which I think padding is considered) would not raise any alarms because, hey, it's just a visual thing!

Just thinking out loud, let me know if it works out.

Anthony
I got really excited by that but unfortunately it does the same thing. What does appear to work, however, is setting a background image on the <a>, eg: background-image: url(images/transparent1x1.gif) - it all seems so unnecessary!
Town
That is really weird! I'd guess that the image is forcing IE to give priority to the link since, almost as if you were putting a layer of plastic over the rest of the row. I sure wish you could just set href tags on any element you wanted.
Anthony
It works with a background-color set as well - IE obviously treats background-color: transparent as exactly that! What's the form for this? Should i give this as the accepted answer because it's got the solution in the comments?!
Town
I won't complain! I'm super jazzed you figured it out. I definitely learned something.
Anthony
A: 

The text wont show-through if the element has a background-color or -image set.

try...

$("a", this).css("background-image", "url(/images/spacer.gif)");

Ben
Haha! Thanks for that, mysterious user.
Town