2

I'm trying to get the source of a tr element using JQuery.

For this I'm using the selector below :

$('#mygrid > table tr')

On console I can see this selector returns me this :

<tr class="red-background">
<td class="date">10/07/2018</td>
<td class="hora">13:09</td>
</tr>

The return is actually an object, not a string :

typeof $('#grid-historicos > table tr')
"object"

enter image description here

So I can't use the method .html()

So what is the right way to get the HTML source of the element? I need only this part as a string :

<tr class="red-background">
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
delphirules
  • 6,443
  • 17
  • 59
  • 108

1 Answers1

1

Working fiddle.

You need to select the tr line you want to target using the index [0] in my example, then select the outerHTML and split the result then finally get the first line :

console.log($('#mygrid > table tr')[0].outerHTML.split('\n')[0]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="mygrid">
  <table style="width:100%">
    <tr class="red-background">
      <td class="date">10/07/2018</td>
      <td class="hora">13:09</td>
      <td>94</td>
    </tr>
    <tr>
      <td>Eve</td>
      <td>Jackson</td>
      <td>94</td>
    </tr>
  </table>
</div>
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101