0

I am trying to make a spreadsheet addon where I have a textarea field where users will be putting the HTML for a table in a field, and I need my script to then take that HTML code, parse it and convert it into an array or object by which I can easily access the table's cells.

The problem I'm facing is that I don't seem to be able to turn the HTML code submitted as text back into a jQuery object I can loop through.

Tl;Dr:

How do I submit a table's HTML code from a form as text and turn it back into an HTML object so I can turn the table into an array/object?

I'm using $("#invoice-info").val() to get its content but using any other methods afterwards gives errors (All of them are either nonspecific or something about "Expected expression but got >", sorry I'm new to JavaScript so I have a hard time debugging it).

Here's the relevant HTML for the form itself:

   <form onsubmit="return(false)">
    <div class="block col-contain">
       <div>
        <textarea class="width-100" id="invoice-info" rows="10"></textarea>
        <label for="invoice-info">Invoice Table</label>
      </div>
    </div>

    <div class="block" id="button-bar">
      <button class="blue" id="make-receipt" onclick='doTest()'>Generate</button>
    </div>
  </form>
j08691
  • 204,283
  • 31
  • 260
  • 272
  • Please show us the logic you have related to the error that you are seeing. – Taplar Feb 06 '19 at 18:04
  • The jQuery you need to do the parsing is in this answer: https://stackoverflow.com/questions/1891357/how-to-iterate-a-table-rows-with-jquery-and-access-some-cell-values – drdwilcox Feb 06 '19 at 18:12

1 Answers1

0

You need to take the result of $("#invoice-info").val() and put it in a domNode. Because it returns a string. var tempDomNode = document.createElement('div'); tempDomNode.innerHTML =$("#invoice-info").val().

So you 'convert' the string into a domNode and then you will be able to use that domNode with or without jQuery to construct your array.

Note : you have to handle the case of a malformed sting (not valid as HTML)

Edit : just found this question on SO : https://stackoverflow.com/a/11047751/1836175 seems to address the same problem.

BenoitVasseur
  • 772
  • 5
  • 8
  • Thanks, I'll try the first solution since i had already tried the parseHTML method but it didn't give any results. I'll post here if I manage to make it work with the temp node. – moulhanout Feb 10 '19 at 13:55