Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to go the old-fashioned pure-JavaScript way, you could do some simple DOM traversal.</p> <p>If you add an <code>id</code> to the table, your life will be much easier, especially if you have (or may later add) other tables in the page.</p> <pre><code>&lt;table id="nameTable"&gt; ... &lt;/table&gt; </code></pre> <p>Now you can access the table in JavaScript and store a reference to it in a variable. You will also want to initialize an empty array/list variable to hold the names later.</p> <pre><code>var nameTable = document.getElementById("nameTable"); var nameArray = []; </code></pre> <p>Next, you begin looping through the child elements of the table, each of which will be a <code>tr</code> (table row) element.</p> <pre><code>for (var i=0; i&lt;nameTable.childNodes.length; i++) { ... } </code></pre> <p>Inside that <code>for</code> loop you will build your list of [first name, last name] pairs. You will do this by grabbing the value of each of the two <code>td</code> (table data cell) children of the current row.</p> <pre><code> var dataCells = nameTable.childNodes[i].childNodes; var namePair = [dataCells[0].innerHTML, dataCells[1].innerHTML]; </code></pre> <p>That should give you a JSON array something like this (my values added):</p> <pre><code>[ ["Andy", "Jackson"], ["Barry", "Obama"], ["Benny", "Franklin"], ["Georgey", "Washington"], ["Billy", "Clinton"] ] </code></pre> <p>To review, the full code would look something like this:</p> <pre><code>var nameTable = document.getElementById("nameTable"); var nameArray = []; for (var i=0; i&lt;nameTable.childNodes.length; i++) { var dataCells = nameTable.childNodes[i].childNodes; var namePair = [dataCells[0].innerHTML, dataCells[1].innerHTML]; } </code></pre> <p><strong>NOTE:</strong> This is fun, but it is likely that the better solution, if you don't mind a hard-coded JSON array in your source code (if you've got the table there anyway it really doesn't matter), is probably to print a <code>json_encode</code>d array in a <code>&lt;script&gt;</code> tag straight from the PHP, like @JayBhatt suggested. It is likely the faster method, and it puts much less stress on the user's browser.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload