0

Here is my DOM:

...
<table>
  <tbody>
    <tr>
        <td>1</td>
        <td><img src="../first"></td>
        <td>John</td>
    <\tr>
    <tr>
        <td>2</td>
        <td><img src="../second"></td>
        <td>Peter</td>
    <\tr>
  </tbody>
</table>
...

Now I want to make this array: (to pass it to the server side and make a .CSV export of it)

var arr = array();
arr = array(array(1,'../first','John'),array(2,'../second','Peter'));

How can I make array above from the DOM ?


Note: I use jquery, So I can get the src attribute by using this:

$('img').attr('src');

4 Answers 4

1

Haven't tested yet. Basically you need to iterate through all <tr> and get each values.

BTW, array should be Array.

var arr = Array();
$('table tr').each(function(index) {
    var id = $(this).children()[0].innerText;
    var imageSrc = $(this).find('img').attr('src');
    var name = $(this).children()[2].innerText;
    arr.push_back(Array(id, imageSrc, name));
}); 
Sign up to request clarification or add additional context in comments.

Comments

0

You can return array using map() and get().

var array = $('tr').map(function() {
  return [$(this).find('td').map(function(i) {
    return i == 1 ? $(this).find('img').attr('src') : $(this).text()
  }).get()]
}).get()

console.log(array)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tbody>
    <tr>
      <td>1</td>
      <td><img src="../first"></td>
      <td>John</td>
    </tr>
    <tr>
      <td>2</td>
      <td><img src="../second"></td>
      <td>Peter</td>
    </tr>
  </tbody>
</table>

Comments

0

First, add an id to your table, to make sure that you do not accidentally send rows from other table to the server:

<table id="request-rows">
  <tbody>
    <tr>
        <td>1</td>
        <td><img src="../first"></td>
        <td>John</td>
    <\tr>
    <tr>
        <td>2</td>
        <td><img src="../second"></td>
        <td>Peter</td>
    <\tr>
  </tbody>
</table>

Then build your array like this:

var arr = [];
$("#request-rows tr").each(function() {
    var tds = $(this).find("td");
    var newElement = [$(td[0]).text(), $(td[1]).find("img").attr("src")];
    arr.push(newElement);
});
//Do something with arr

Comments

0

Try like this .create array of each tr and then push with main array

var res = []

var a = $('tbody tr')
a.each(function(a, b) {
  a = [];
  a[0] = $(this).children('td').eq(0).text();
  a[1] = $(this).children('td').eq(1).children('img').attr('src');
  a[2] = $(this).children('td').eq(2).text();
  res.push(a);
})
console.log(res)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tbody>
    <tr>
      <td>1</td>
      <td><img src="../first"></td>
      <td>John</td>
      <tr>
        <tr>
          <td>2</td>
          <td><img src="../second"></td>
          <td>Peter</td>
          <\tr>
  <\tbody>
<\table>

1 Comment

typo : near tr tag <\tr>

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.