3

I have a table and this code to find the a href and make a whole row clickable:

$(document).ready( function() {

        $('table tr').click(function() {
            var href = $(this).find("a").attr("href");
            if(href) {
                window.location = href;
            }
});

However, there is one problem and that's my right click do not offer me "Open link in new tab" option and window etc. stuff like on a normal a href link.

Using divs instead of table is not an option.

How to fix it?

Is there any jQuery plugin that ca nfix it for me?

My table looks like this (it's much bigger with more td's but just for illustration):

<table>
    <tr>
         <td>
             <a href="http://example.com">Some link</a>
             <span>Bla bla bla</span>
         </td>
         <td>
             <span>Some text</span>
         </td>
    </tr>
    <tr>
         <td>
             <a href="http://someotherlink.com">Some link</a>
             <span>Other text</span>
         </td>
         <td>
             <span>Something else</span>
         </td>
    </tr>
    <tr>
         ...
    </tr>
</table>

EDIT: I need to grab the a href value automatically like in my current code: $(this).find("a").attr("href");

EDIT 2: I need to be the whole tr clickable (as a block). which is possible using the method above. However, it is not possible to click on the row and select "Open link in a new tab". This option is available only when I hover over the a href. But I need it to be also on the whole row. So, if the user wants to open multiple new tabs using middle mouse button or right click and selecting "Open in a new tab" from the context menu, he can do it. Right now it is not possible.

3
  • Do you mean that you want the right-click link options like "Open Link in New Tab" to apply to the entire table row? Commented Aug 8, 2012 at 5:18
  • @Derfder the obvious solution is to have every column in the row to be a link. You can style them however you want. Commented Aug 8, 2012 at 6:13
  • Please verify my solution. I think is the best for you Commented Aug 8, 2012 at 6:31

2 Answers 2

2

Why don't you try something like this:

JS:

$(document).ready(function(){
    $("table tr").mousedown(function(e){

        document.oncontextmenu = function() {return false;}; 

        e.preventDefault();

        var href = $(this).find("a").attr("href");
        if( e.button == 2 ) {
            $(this).find('td:first').prepend('<div class="blank"><a href="' + href + '" target="_blank">Open in new window</a></div>').find('.blank').css('left',e.pageX + 'px').css('top',e.pageY + 'px').fadeIn(400);

        } else if(e.button === 0) {
            if(href) { 
                window.location = href;
            }
        } 
    });

    $(document).on('click','.blank a',function(){ 
        $(this).parent().fadeOut(400,function() { 
            $(this).remove(); 
        }); 
    }).on('mouseleave','.blank',function(){  
        $(this).fadeOut(400, function(){ 
            $(this).remove();     
        });     
    }); 

});

CSS:

.blank {
    display:block;
    padding:4px;
    background:#c3c3c3;
    position:absolute;
}

table tr td {
    overflow:visible;
    position:relative;
}

table tr {
    cursor:pointer;
}

Here is a jsfiddle, but note that I didn't stylized the appearance and the javascript code must be updated regarding the left button click.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I think it is the only possibility how to deal with it.
Old post, I just wanted to thank everyone for this. I've been pulling my hair out with a problem using .click, so I switched to $().on('click', etc - and everything works fine...Thanks!
0

Try this

    $('tr th, tr td').wrapInner('<a href="http://example.com"></a>').children('a').click(function() {
   var href = $(this).attr("href"); if(href) { window.location = href; }
});

//If there is any delegation issue try this

    $('tr th, tr td').wrapInner('<a class="links" href="http://example.com"></a>');
$('body').delegate('a.links','click',function(e){
e.preventDefault();

   var href = $(this).attr("href"); if(href) { window.location = href; }
});

5 Comments

Would it be html5 valid? Btw. I need it to grab the href value automatically like: var href = $(this).find("a").attr("href"); if(href) { window.location = href; }
Yes it is valid HTML5 and you need to change your code to var href = $(this).attr("href"); if(href) { window.location = href; } and replace my alert message
OK, I will try it now and let you now if it works in 10 minutes ;)
Thanks, but your code will make all the links target to example.com and even span became links :(. I do not want that. I just need to be the tr clickable. which is possible using the method. In my original post. However, it is not possible to click on the row and select "Open link in a new tab". This option is available only when I hovver over the a href, but I need it to be also on the whole row. So, if the user wants to open multiple new tabs using middle mouse button or right click and Open in a new tab, he can do it. Right now it is not possible.
@Derfder - You could wrap everything in anchors as ubercooluk suggests but then style the anchors so that look like normal text (and remove them from the tab order). I don't think there's any way to make an anchor's right-click menu appear for other elements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.