1

I am new in MVC. I want to add my HTML Table data into JSON array object. and that object I want to pass to Action Using ajax post.

I am not able to convert my html rows to json object. Can Anybody have solution for the same It will be appreciable

thanks In advance

1 Answer 1

5

HTML

     <table id="table">
      <tr>
        <td>Alfreds Futterkiste</td>
        <td>Maria Anders</td>
        <td>Germany</td>
      </tr>
      <tr>
        <td>Centro comercial Moctezuma</td>
        <td>Francisco Chang</td>
        <td>Mexico</td>
      </tr>
      <tr>
        <td>Ernst Handel</td>
        <td>Roland Mendel</td>
        <td>Austria</td>
      </tr>
      <tr>
        <td>Island Trading</td>
        <td>Helen Bennett</td>
        <td>UK</td>
      </tr>
      <tr>
        <td>Laughing Bacchus Winecellars</td>
        <td>Yoshi Tannamuri</td>
        <td>Canada</td>
      </tr>
      <tr>
        <td>Magazzini Alimentari Riuniti</td>
        <td>Giovanni Rovelli</td>
        <td>Italy</td>
      </tr>
    </table>

JS code to create JSON Array

  var table = document.getElementById('table');
  var jsonArr = [];
  for(var i =0,row;row = table.rows[i];i++){
       var col = row.cells;
       var jsonObj = {
           company : col[0].innerHTML,
           contact : col[1].innerHTML,
           country : col[2].innerHTML
         }

      jsonArr.push(jsonObj);
  }


  console.log(jsonArr);

https://jsfiddle.net/0h957km9/3/

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

Comments

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.