How to enable click on entire cell of a table opening file on a new page

Asked

Viewed 242 times

1

I’m trying to adapt a code that should do the following, by clicking a cell of a table trigger the link by opening the file in a new browser window, what I have so far is this, on tr of table one data-url calling a function. Code works, but does not open in a new window

Table with function call:

<tr data-url="<?php echo $DadosFichas->Url; ?>">
  <td><?php echo $DadosFichas->Nome; ?></td>
</tr>

Function called:

$(document).ready(function(){
    $('table tr').click(function(){
        window.location = $(this).data('url'), target = "_blank" ;
        return false;
    });
});
  • Why not use the element itself a HTML with the attribute target="_blank"?

  • what happens? opens in the same tab? does not open?

  • Hm tested: window.open(url, '_Blank'); but also wanted to know the same as @Andersoncarloswoss

  • Hi, @Andersoncarloswoss and Rafael Acioly, thanks for asking, responding to Anderson, I need to make available the option to click on the whole cell and Rafael, the page opens but in the same tab.

3 answers

2


CSS (not required)

.tdClicavel {
   cursor: hand;
}

Script

    $(document).ready(function() {

    $('#exemplo tr td').click(function() {
        var href = $(this).find("a").attr("href");
        if(href) {
            window.open(href,'_blank');
        }
    });

});

HTML

Table with id = example

<table id="exemplo">

Cell clickable with class = tdClicable (not required), but mandatory tag a as exemplified

<td class="tdClicavel" bgcolor="#CCCCCC">
 <a href="/unanswered/tagged/?tab=newest"></a> 

 ..............................
 .............................
  .........................</td>

In your case the td should be like this:

<td class="tdClicavel"><a href="<?php echo $DadosFichas->Url; ?>"></a><?php echo $DadosFichas->Nome; ?></td>
  • Hello @Leo Caracciolo, in my case I don’t have the href.

  • teaches me this magic of clicking something without url open a window.

  • I think I expressed very badly, I’m using data-url in your case is catching the attr href.

  • Thanks @Leo Caracciolo, solved my problem.

  • I edited the answer by putting example of how the td should be for your case

1

you can use the function window.open(), being as follows;

$('td[data-url]').on('click', function(){
    var url = $(this).data('url');
    window.open(url, '_blank');
});

1

Put the data-url in the td there instead of tr:

<tr>
  <td data-url="<?php echo $DadosFichas->Url; ?>"><?php echo $DadosFichas->Nome; ?></td>
</tr>

Use the window.open in place of location and tag td in front of the tr:

$(document).ready(function() {
  $('table tr td').click(function() {
    window.open($(this).data('url'), '_blank');
    return false;
  });
});
  • Hello @Wendel Rodrigues, I took the test here and it didn’t work, thanks.

  • I changed my answer, did some tests here and it worked. Take a look there.

  • Show, I just need one more tip, what can I do when I have more than one url on the same page? Just notice it here.

  • I don’t quite understand your question.

  • Is that now I have menu with letters, type A B C that the user clicks to call the chips, to then click on the table cell and open the file on another page. Only now if the user chooses a letter is opening a new tab

  • Are you saying that after you added this code all the links started to open in a new tab? This menu you did using table too? I still don’t understand

  • Exactly that.

  • To apply this rule only to a specific table, set an id for it <table id="dadosFicha"> and add in the code $('table#dadosFicha tr td').click(function() {

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.