How to enable click on every cell in a table by opening a file on a new page

1

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

Table with the function call:

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

Call function:

$(document).ready(function(){
    $('table tr').click(function(){
        window.location = $(this).data('url'), target = "_blank" ;
        return false;
    });
});
    
asked by anonymous 21.06.2017 / 22:20

3 answers

2

DEMO

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">

Clickable cell with class = tdClicavel (not necessary), but mandatory to place the tag as exemplified

<td class="tdClicavel" bgcolor="#CCCCCC">
 <a href="https://pt.stackoverflow.com/unanswered/tagged/?tab=newest"></a> 

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

In your case, the td should look like this:

<td class="tdClicavel"><a href="<?php echo $DadosFichas->Url; ?>"></a><?php echo $DadosFichas->Nome; ?></td>
    
21.06.2017 / 23:26
1

You can use the window.open() function as follows;

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

Place data-url in td instead of tr :

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

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

$(document).ready(function() {
  $('table tr td').click(function() {
    window.open($(this).data('url'), '_blank');
    return false;
  });
});
    
21.06.2017 / 22:30