Solution for displaying table cells in IE 7 and lower using Javascript

Lately, the community has come up with some amazing tools to push early versions of IE beyond their intended capabilities. For example, using multi-column CSS selectors. However, I've been struggling to find a JavaScript that can be loaded conditionally in IE lte 7 to convert multi-column layouts that utilize display: table-cell.

Does anyone have knowledge of such a script?

<div id="sidebar">
   <ul id="main_nav">
      <li><a href="about_us.php">ABOUT US</a></li>
   </ul>
</div> <!--end of sidebar-->

<div id="content">
   <div id="main_content">
     <h1>Header</h1>
     <p>Page Content</p>
   </div> <!--end of main_content-->

   <div class="aside_info">
      <h2>Side Info</h2>         
   </div>
</div> <!--end of content-->

We have 3 columns - #side_bar and #content serve as columns, while within #content there is #main_content and #aside_info. This setup allows for a 3 column layout on pages with an #aside_info div, but defaults to two columns on other pages.

If you happen to know of a script that could convert this layout to tables for outdated browsers, please do share!

Thank you,

Daniel

Answer №1

Transforming this task into a simple process with the power of jQuery:

$('<table><tr><td></td><td></td><td></td></tr></table>')
    .find('td')
        .eq(0)
            .append( $('#sidebar') )
            .end()
        .eq(1)
            .append( $('#main_content') )
            .end()
        .eq(2)
            .append( $('.aside_info') )
            .end()
        .end()
    .appendTo( $('#content') );

I trust this guidance proves useful!

Answer №2

After working on a solution for this issue, I returned to find that jimbojw had devised a much more elegant and refined solution. I'm giving him credit because the simplicity of his approach is truly appealing (note to self: brush up on jQuery skills!). Nonetheless, I'm sharing my solution here in case jQuery isn't an option, testing for IE7 is unfamiliar territory, or if an alternative approach is needed:

<div id="sidebar">
   <ul id="main_nav">
      <li><a href="about_us.php">ABOUT US</a></li>
   </ul>
</div> <!--end of sidebar-->

<div id="content">
   <div id="main_content">
     <h1>Header</h1>
     <p>Page Content</p>
   </div> <!--end of main_content-->

   <div class="aside_info">
      <h2>Side Info</h2>         
   </div>
</div> <!--end of content-->

<script type="text/javascript">
    // Check for IE version
    var ieversion = 0;
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ 
        ieversion = new Number(RegExp.$1)
    }   

    if(ieversion > 0 && ieversion < 8)
    {
        // Locate existing DIV elements
        var sidebarDiv = document.getElementById("sidebar");
        var mainContentDiv = document.getElementById("main_content");
        var contentDiv = document.getElementById("content");

        // Create table structure to replace them
        var tableElement = document.createElement("table");
        var tbodyElement = document.createElement("tbody");
        var trElement = document.createElement("tr");
        var sidebarTd = document.createElement("td");
        sidebarTd.id = "sidebar";
        var mainContentTd = document.createElement("td");
        mainContentTd.id = "main_content";
        var asideInfoTd = document.createElement("td");

        // Clone child nodes from DIVs and add them to appropriate TDs, then include TDs in TR.
        for(var i=0;i<sidebarDiv.childNodes.length;i++)
        {
            sidebarTd.appendChild(sidebarDiv.childNodes[i].cloneNode(true));
        }
        trElement.appendChild(sidebarTd);

        for(var i=0;i<mainContentDiv.childNodes.length;i++)
        {
            mainContentTd.appendChild(mainContentDiv.childNodes[i].cloneNode(true));
        }
        trElement.appendChild(mainContentTd);
        mainContentDiv.parentNode.removeChild(mainContentDiv);

        var contentChildDivs = contentDiv.getElementsByTagName("div");
        for(var i=0;i<contentChildDivs.length;i++)
        {
            if(contentChildDivs[i].className.indexOf("aside_info") > -1)
            {
                asideInfoTd.appendChild(contentChildDivs[i].cloneNode(true));
            }
        }
        // Only add aside info if available
        if(asideInfoTd.childNodes.length > 0)
        {
            trElement.appendChild(asideInfoTd);
        }

        // Finalize table assembly
        tbodyElement.appendChild(trElement);
        tableElement.appendChild(tbodyElement);

        // Remove current content div and replace sidebar with new table containing 2-3 columns.
        contentDiv.parentNode.removeChild(contentDiv);
        sidebarDiv.parentNode.replaceChild(tableElement, sidebarDiv);
    }
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Interpolating UV is not allowed: 'flat': Using a reserved word in an illegal manner

As a beginner, I recently learned that UV coordinates are not interpolated in WebGL, allowing for exact pixel values to be retrieved. However, when attempting to use the 'flat' keyword, I encountered an error. By adding 'flat' before t ...

A method for enabling a stationary, fluctuating height object to occupy area

Initially, I am looking for a solution using only CSS. While JavaScript can accomplish this easily, I prefer to stick to CSS for now. On my webpage, I have three containers: two fixed and one static. The goal is to adjust the padding of the static contai ...

What is the process for opening and populating dialogs from dynamically mapped cards?

I have a unique array of JSON data that is connected to Material-UI (MUI) cards. Each card features a button that triggers a dialog to open. When clicking the button on a card, I aim to pass the specific value of GroupName into the dialog. In my case, ther ...

Having trouble with showing dynamic data in column2 of my HTML layout, and the alignment doesn't look quite right in HTML

I'm working with this HTML file, attempting to create a two-column layout using HTML and CSS. I want one column to be labeled REQUEST and the other RESPONSE. When a value is entered in the text field in Column 1, it should display a response in Column ...

The significance of the "$=" or "?=" symbols in lit-element illustrations

I'm struggling to comprehend the purpose of ?= or $= in these two instances: First Example: Lit-Element README <div id="box" class$="${this.uppercase ? 'uppercase' : ''}"> <slot>Hello World</slot> </div> ...

Take a sequence of multiple words and combine them into a single string with hyphens in between

I've been working on a WordPress website and I created a JavaScript function that adds a class to the body tag, allowing for different backgrounds based on the category. It works perfectly fine when the category is a single word, but encounters issues ...

Attempting to replicate the action of pressing a button using Greasemonkey

I am currently working on a greasemonkey script to automate inventory updates for a group of items directly in the browser. I have successfully implemented autofill for the necessary forms, but I am facing challenges with simulating a click on the submissi ...

My media queries refuse to cooperate until I add the magical "!important" declaration

Utilizing Bootstrap 4 along with a custom CSS file on my website. Within the CSS file, I have included some media queries at the bottom: @media (max-width: 575px) { .address .contact { text-align: center; } } /* Small devices (tablets, 768px and ...

Asynchronous requests in Node.js within an Array.forEach loop not finishing execution prior to writing a JSON file

I have developed a web scraping Node.js application that extracts job description text from multiple URLs. Currently, I am working with an array of job objects called jobObj. The code iterates through each URL, sends a request for HTML content, uses the Ch ...

What is the most efficient way to retrieve key pair values of separate elements in an array with just one API request?

The API I am working with returns an array of elements, each containing multiple key-value pairs. An example of a similar API response can be seen here: , where an array of poems is returned. [ { "title": "...." "content": "..." "url" : "..." } ...

Whenever I implement JavaScript validation on my HTML page, it causes the page to become un

Whenever I try to enter more than 30 to 40 characters in the password input field on my HTML page, all web browsers become unresponsive. This issue persists even if I modify the minimum length and other requirements to 50. I am new to JavaScript. <!D ...

Using TypeScript, you can pass an object property name as a function argument while ensuring the type is

How can I define a type in a function argument that corresponds to one of the object properties with the same type? For instance, if I have an object: type Article = { name: string; quantity: number; priceNet: number; priceGross: number; }; and I ...

Tips for eliminating the pesky "ghost" line that appears in your coded HTML email

Check out this Sample Code: <html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office"> <head> <title></title> & ...

Accessing an array outside of an Ajax call that was initiated within a success callback

I have a CSV parsing function written in JavaScript that extracts movie names from a CSV file using an Ajax call within a loop. movies = new Array(); for (var i = 1; i < allData.length; i++) { var mName = allData[i][0]; var mPath = allData[i ...

What causes my input field to lose focus in React.js after typing just one character?

My react.js component is experiencing an issue where the input field loses focus whenever a character is typed. To continue typing or editing, I have to click on the input field again. What could be causing this problem? Below is the code snippet in quest ...

Ways to swap webpack-dev-server for alternative servers such as express or apache

After using vue-cli to scaffold a Vue app, I am looking to set up a new web server instead of webpack-dev-server. I am interested in having a separate configuration file to customize the server settings, similar to using Node Express for production deploym ...

Retrieve the HTML code from a webpage on a different domain

Looking to extract HTML from another domain. Attempted solution: $.get("http://website1.com", function(data) { alert("Data Loaded: " + data); }); (not working) For example, as the owner of two websites: website1.com website2.com Now, the goal is to ret ...

Setting model value in Angular 2 and 4 from loop index

Is it possible to assign a model value from the current loop index? I've tried, but it doesn't seem to be working. Any suggestions on how to achieve this? Check out this link for my code <p *ngFor="let person of peoples; let i = index;"& ...

Accessing a distinct element of e.parameter

I've implemented a Google App Script on my website that automatically writes form data to a spreadsheet upon submission. With multiple forms on the site, I want all their data to go to the same Spreadsheet but different 'Sheets' (tabs at th ...

I'm struggling to make my div display inline

My div elements are not displaying inline, I'm thinking maybe I shouldn't use the nav and div structure like this. But starting over again seems like a lot of work. How can I fix this issue? Just so you know, I'm only on my 4th day of learn ...