2

I don't know if this is possible, and I've been digging around for code for hours with no luck, so I'm going to give this a shot.

Let's say I have a table on a page. It contains the following:

<table id="srctb">
    <tr>
        <td>Users</td>
        <td>Musers</td>
        <td>Abusers</td>
    </tr>
    <tr>
        <td>50</td>
        <td>34</td>
        <td>16</td>
    </tr>
</table>

On a different page, I have another table. It looks like this:

<table id="desttb">
    <tr>
        <td>Users</td>
        <td>Musers</td>
        <td>Abusers</td>
    </tr>
    <tr>
        <td> </td>
        <td> </td>
        <td> </td>
    </tr>
</table>

Without having to manually enter the values, like they are in the srctb table, I want to populate table desttb with the exact same values.

Is there a piece of javascript code that will fetch the value of a cell in table srctb, and stick it into a cell in table desttb?

Any help is greatly appreciated!

EDIT: If there isn't a way to do it with javascript, I do believe I can use jquery to a certain extent, but I'm not sure. I know there are better ways to do this, but unfortunately the site where this needs to work is rather limited in capability.

Luke
  • 22,826
  • 31
  • 110
  • 193
  • In other words you want to copy the content of table `srctb`? – hindmost Oct 16 '15 at 15:01
  • 2
    what do you mean by "on a different page": is that really new browser page loaded after the one with `srctb` in it? – Axel Amthor Oct 16 '15 at 15:06
  • @hindmost Yes, I would like to copy the content of table srctb to table desttb. AxelAmthor The desttb is coded into the html layout of the site. The content will need to be updated frequently. Unfortunately, I'm the only one who can go in to the html coding, and update the desttb. The srctb, however, is - for lack of a better description - pasted as code in a post on the 'front end' of the site. It's much easier to edit for the people who work with me since they don't have to dig through html to get to it. So, when they update that table, it needs to pull through automatically to the desttb. – Vex Vaudlain Oct 16 '15 at 15:16
  • 2
    If these tables are on different pages there is no way to do such copy by javascript. You have to do this by server-side means. – hindmost Oct 16 '15 at 15:23
  • @AxelAmthor (sorry, screwed up the tagging with the first reply) The desttb is coded into the html layout of the site. The content will need to be updated frequently. Unfortunately, I'm the only one who can go in to the html coding, and update the desttb. The srctb, however, is - for lack of a better description - pasted as code in a post on the 'front end' of the site. It's much easier to edit for the people who work with me since they don't have to dig through html to get to it. So, when they update that table, it needs to pull through automatically to the desttb. – Vex Vaudlain Oct 16 '15 at 15:26
  • still not clear what "html layout" and "frontpage" means – Axel Amthor Oct 16 '15 at 15:30
  • @AxelAmthor It's a forum so you have your usual html where you would do the layout, the skinning, etc - the place where code usually goes. This is where the destination table will go. But because it is a forum, there are places where you can make posts - and in such a post, I can stick the html code for the source table. That table will contain data that needs to pull through to the destination table. In short - I need cell data from table 1 (on specific url) to pull through to the cells of table 2 (no url). – Vex Vaudlain Oct 16 '15 at 15:39

2 Answers2

0

You could save the content of the table in a localstorage object.

page 1:

window.onbeforeunload = function() { // when you switch page
  localStorage.setItem("content", document.getElementById("from").innerHTML); // save the data to localstorage
};

page 2:

window.onload = function () {
  var content = localStorage.content;
  if (content !== undefined) {
    document.getElementById("to").innerHTML = content;
  }
}

I hope this works!

code example

nloomans
  • 220
  • 2
  • 11
  • No luck I'm afraid, but that you for trying! – Vex Vaudlain Oct 16 '15 at 15:51
  • @VexVaudlain Are you using a server/localhost? – nloomans Oct 16 '15 at 16:11
  • This is very possibly user error. i just want to make sure - I'm only installing the code quoted above, yes? And I can install it anywhere in the code on pages 1 and 2? – Vex Vaudlain Oct 16 '15 at 16:40
  • @VexVaudlain can you please clarify your question? I'm using the files in "code example" and using `python3 -m http.server` to serve it. – nloomans Oct 16 '15 at 16:57
  • @myCyborg damn... yea, that would have worked, if the site had the capability. Like I mentioned before, it is very limited in what it can and can't do. I think I'm going to have try something different instead. THANKS for all the help! – Vex Vaudlain Oct 16 '15 at 19:08
0

Assuming you can use jQuery 1.8 or later, you should be able to do something like this:

$.get('/srctab.html').done(function(data) {
  $.parseHTML(data).find('#srctb > *').appendTo('#desttb');
});

This grabs the other page using AJAX, parses it as HTML, finds descendants of #srctb, and appends them to #desttb.

I haven't tested this yet, and it may run afoul of the idea that each table has an implicit or explicit tbody that contains the rows.

Also, if you need the actual data values instead of the whole row, you'll have to use a few more complicated selectors to get them, but the basic idea of grabbing the page and parsing it still applies.

Samuel Edwin Ward
  • 6,526
  • 3
  • 34
  • 62