0

I am trying to convert the HTML of a basic table into a basic <section> tag. I am having trouble iterating each table, and the "content" div is being repeated into each section. See my fiddle:

$(document).ready(function() {
    var section = $("<section>");
        $("table").each(function(){
            $("td").each(function(){
                var content = $('<div class="content">').html(this.innerHTML);
                section.append(content);
            });
        $("table").replaceWith(section);  
    });
});

http://jsfiddle.net/zre0qof1/

The results I need is:

<section>
<div class="content">
<h2>About</h2>
</div>
</section>

<section>
<div class="content">
<h2>Services</h2>
</div>
</section>

I know it's probably really easy and just a matter of iterating the sections/tables correctly, but I just can't seem to get it right. Thanks in advance!

CBroe
  • 91,630
  • 14
  • 92
  • 150
Joseph Thomas
  • 137
  • 1
  • 12
  • Could you better clarify what the end result should be? Is there only ever one section and each table turns into a div.content ? Would be easiest if you just posted the result HTML syntax you would like to have in the end – Talya S Oct 09 '15 at 22:59

1 Answers1

1

From your example I understand that each table turns into
<section><div class="content"> ALL TABLE CONTENT </div></section> You only have one TR and one TD in each table, so it's hard to tell if you want something else.

If this is the case this code does the trick:

$(document).ready(function() {
    $("table").each(function(){ // go through every table
        var content = $('<div class="content"></div>'); // create a content div
        $("td", this).each(function(){ // go through every TD in table
            content.append(this.innerHTML); // insert TD content into content div
        });
        $(this).replaceWith($('<section></section>').append(content)); // put content div in section and replace this table with it
    })
});

Here's the fiddle: http://jsfiddle.net/jtw97u1f/5/

I looked it up and when creating elements you should include the closing tag, so I changed it to $('<section></section>'). Here's the answer I used as reference: https://stackoverflow.com/a/6555916/5297207

Community
  • 1
  • 1
Talya S
  • 734
  • 5
  • 20
  • Talya, thank you SO much! This is EXACTLY what I needed! :) http://dev586.webdugout.com/ – Joseph Thomas Oct 09 '15 at 23:26
  • Awesome :) I'm curious why you would need to do this instead of editing the source HTML – Talya S Oct 09 '15 at 23:30
  • Well, my site's are on a custom CMS...and my customers, well - they tend to mess up the code pretty easily. I figured if I code the sections to be simple tables, there's a better chance the content will stay intact - and when the live site is view, it's all converted to clean sections. :) Thanks again! – Joseph Thomas Oct 10 '15 at 03:40
  • The thing is, I'm not sure what you're gaining from this. The main complaint about layouting with tables has always been that they're slow to render- and here you're still rendering them then replacing them on the client side, which is only more work for the browser. If it's the way the tables are displayed that's the issue, `display:block;` works on table elements too... although, with only one cell in each table I'm not sure what trouble they could be giving you. Anyway sorry for butting in. I wouldn't object if you marked my answer as accepted by the way... :) – Talya S Oct 10 '15 at 09:18
  • I am gaining peace of mind when my customer goes into the backend of their site and they are editing their content within tables, instead of sections. I fear that if they mess with the content too much, they will end up removing the section tags and I feel there's less of a chance of them messing up the html if they have highly visible bounding boxes that the tables create in the CMS. In the end, the tables are converted into sections, which is much cleaner - plus I have a ton of jQuery scripts depending on those section tags. – Joseph Thomas Oct 10 '15 at 23:55
  • I understood the part about the CMS and tables being easier on your customers, I wasn't arguing against using tables, just about turning them into sections. There is nothing clean about running extra scripts on the client side, my point was that keeping the tables *would be cleaner*. If you have scrips depending on the section tags however, then so be it. (Make sure those scripts don't try to find the section tags *before* your replacement script runs...) – Talya S Oct 11 '15 at 00:02
  • Thanks for the tips Talya. I'm still kind of new when it comes to jQuery, but I don't seem to have any loading(?) issues with my sites that contain a lot of JS, so I never thought that the extra scripts being ran are doing anything negative to the site's performance - but maybe I'm right? And no worries, I got the order down for the scripts! :P – Joseph Thomas Oct 12 '15 at 03:26
  • Yeah you wouldn't notice the difference on most devices, just like you wouldn't notice that rendering `` tags takes longer. It's about the "right way" to do things more than anything a user can really notice. Just like layouting with tables works, but it's still a bad habit, so is trying to do everything with jQuery. The way you're doing things obviously works for you, I'm just pointing out it might not be ideal. I'm always trying to find the most "correct" way to do things. :)
    – Talya S Oct 12 '15 at 12:31
  • Again, thanks for the tips Talya! By the way, I decided against using the script anyway - at least for now! Ya know, I wouldn't use so much jQuery if I wasn't so limited with how the HTML is laid out in my CMS. You seem like you'd make a very good contact for things like this, I would love to be able to reach out to you if I had trouble in this realm again. – Joseph Thomas Oct 13 '15 at 15:12
  • @JosephThomas Glad I made a good impression :) I wouldn't mind helping you in the future but I don't want to post my email here. If you'd like to write some way to contact you I can send you my information. – Talya S Oct 13 '15 at 16:39
  • A VERY good impression! :) You can reach me at smallminddesigns.com! – Joseph Thomas Oct 14 '15 at 18:21