Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7996699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:46:20+00:00 2026-06-04T14:46:20+00:00

I am trying to convert all tables to divs and add the th html

  • 0

I am trying to convert all tables to divs and add the th html or contents of the th in from of each td that is converted to a div.

So my table would look like this:

<table>
    <tr>
      <th>Title 1</th>
      <th>Title 2</th>
      <th>Title 3</th>
      <th>Title 4</th>
      <th>Title 5</th>
    </tr>
    <tr>
      <td>Data 1</td>
      <td>Data 2</td>
      <td>Data 3</td>
      <td>Data 4</td>
      <td>Data 5</td>
    </tr>               
</table>

And I want to convert it so it is like this:

<div class="box">
<div class="row-fname"><span class="fname-label">TH Data</span> : <span class="fname-data">Data 1</span></div>
<div class="row-lname"><span class="lname-label">TH Data</span> : <span class="lname-data">Data 2</span></div>
<div class="row-address"><span class="address-label">TH Data</span> : <span class="address-data">Data 3</span></div>
<div class="row-city"><span class="city-label">TH Data</span> : <span class="city-data">Data 4</span></div>
<div class="row-state"><span class="state-label">TH Data</span> : <span class="state-data">Data 5</span></div>

I found this code that almost works but can figure out how to make it get the th if there are any th’s and add them before each td’s data span.

$('.content table').replaceWith(function() {
    var html = '';

    $('tr', this).each(function() {         
        html += '<div class="box grey-bg">';
        $('th, td', this).each(function() {
            html += '<span>' + $(this).html() + '</span>';
        });
        html += '</div>';
    });

    return '<div class="">' + html + '</div>';
});

Any help would be soooo greatly appreciated!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:46:21+00:00Added an answer on June 4, 2026 at 2:46 pm

    This works:

    $('.content table').replaceWith(function() {
    
        var $th = $(this).find('th'); // get headers
        var th = $th.map(function() {
            return $(this).text();
        }).get(); // and their values
    
        $th.closest('tr').remove(); // then remove them
    
        var $d = $('<div>', { 'class': 'box' });
    
        $('tr', this).each(function(i, el) {
            var $div = $('<div>', {'class': 'inner'}).appendTo($d);
            $('td', this).each(function(j, el) {
                var n = j + 1;
                var $row = $('<div>', {
                    'class': 'row-' + n
                });
                $row.append(
                $('<span>', {
                    'class': 'label-' + n,
                    text: th[j]
                }), ' : ', $('<span>', {
                    'class': 'data-' + n,
                    text: $(this).text()
                })).appendTo($div);
            });
        });
    
        return $d;
    });
    

    demo at http://jsfiddle.net/alnitak/UK395/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am simply trying to convert a table from MyISAM to INNODB. This is
Basically on .show() I've been trying to have all of the inputs convert to
So I'm trying to convert a web service that was an Oracle application to
I'm trying to convert the permanent tables used in a stored procedure to global
I'm trying to convert a project that is currently using a custom DAO framework
I want to pull data from table Province_Notifiers and also fetch all corresponding items
I'm trying to understand what is the best practice using XSD tables generated from
I am trying to convert one of DB table row in to column and
I have a problem, I'm trying to convert an SQLQuery object result (from Hibernate)
i´m using subsonic 3 trying convert a SQL2008 project to MySQL. when the projects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.