Creating a Responsive Div Wrapper for Table Cells using JQuery

I have a SharePoint table table#layoutsTable that is automatically generated. I managed to wrap the entire table with a div.row element. I am now attempting to assign specific classes to individual td elements using columns + .large-# based on the width property of each cell. Here's the abbreviated HTML code snippet:

<span id="DeltaPlaceHolderMain">
    ...
        <div class="row">
            <table id="layoutsTable" style="width: 100%;">
                <tbody>
                    <tr style="vertical-align: top;">
                        <td style="width: 66.6%;">
                            ...
                        </td>
                        <td style="width: 33.3%;">
                            ...
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    ...
</span>

The manual wrapping of <div class="row"> into the markup has been executed using jQuery. The code snippet below shows my attempt to add classes to individual cells:

$(document).ready(function(){
    $('table#layoutsTable').wrap('<div class="row">'); //works correctly

    $('table#layoutsTable td').each(function(){
        var width = $(this).css('width');
        console.log(width);
        if(width === '66.6%'){
            $(this).addClass('large-8 columns');
        }

        if(width === '33.3%'){
            $(this).addClass('large-4 columns');
        }
    });
});

Another approach (though limited for responsiveness) is directly adding these responsive classes to the cells themselves which is not my preferred method.

Answer №1

In my opinion, it is not ideal to enclose a td element within a div. This action undermines the purpose of utilizing the table structure. Instead, simply assign the desired class directly to the td element.

$(this).addClass("large-8 columns");

http://api.jquery.com/addClass/

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

jQuery: searching for the ancestors of an element

Can anyone suggest a more efficient method for selecting grandparent elements using jQuery to eliminate the repetition? $(this).parent().parent().parent().parent().parent().children(".title, .meta").fadeIn("fast"); Appreciate any advice. Thank you. ...

What methods are available to create a transition animation for an HTML 5 banner on iOS devices?

I need assistance with animating an HTML5 banner to transition vertically onto the screen in my app demo. After a prompt that triggers a server callback, the banner should smoothly move down from the top of the screen. The animation I am aiming for is simi ...

Polymer elements fail to adapt to varying screen sizes

I am currently working on creating a responsive website using polymer, but I have encountered an issue where certain elements (such as core-toolbar and paper-fab) are not scaling properly on smaller, denser screens like smartphones. After browsing through ...

Filter searching for list results by text input

I have extracted a list of contacts from a MySQL database and showcased it using PHP/HTML/CSS. My goal is to implement a filter where users can type in a textbox and see the contact list narrow down based on the entered text. For reference, something simi ...

What is the best way to create a page that moves on release using HTML/CSS?

I'm working on a website where I want to make an interactive feature. When users click on an image on the left side of the page, I want the page to move backward. Similarly, when they click on an image on the right side, I want the page to move forwar ...

Tips for utilizing the two-function in jQuery to showcase one DIV following another DIVNote: When using the two-function

Apologies in advance for my poor English skills, but I have been contemplating whether to write here and if you will be able to understand me. I am currently working on a jQuery script: $(function() { $('#div1').hover(function() { $ ...

What technique works best for changing the visibility of an image without causing other elements to shift position?

On my webpage, I have a table cell with an image that should only display when hovering over the cell. The problem is that when the image is shown, it shifts the other text to the left because its default state is "display:none". I'm searching for a s ...

Is it possible to activate the identical drop-down or popover with multiple buttons?

Is it possible to activate the same drop-down menu with multiple buttons? For example, I want a button at the top of the page labeled special menu, another in the middle also labeled special menu, and one at the bottom as well labeled special menu. When a ...

What is preventing me from combining JavaScript functions with jQuery methods?

As someone who is fairly new to using jQuery (although proficient in regular JavaScript), I am facing a significant challenge that I just can't seem to crack. Typically, I am able to find answers on my own, but this particular issue has persisted for ...

Error: operand a contains an invalid 'in' statement

After sending a request to the server using jQuery's $.ajax, I am getting back JSON data. $.ajax({ url: 'moreMonth.ajax', data: { startIndex: id }, success: function(data) { $.each(data, function(k, v) { alert(k + ':&ap ...

Is it possible to add animated text using anime.js?

var elements = document.querySelectorAll('.content'); anime({ targets: elements, content: 100, }); <script src="https://raw.githubusercontent.com/juliangarnier/anime/master/lib/anime.min.js"></script> <span class="content"> ...

ASP.net is encountering difficulty locating a specific JavaScript function

Seeking assistance to comprehend the issue, I have devoted countless hours scouring Google for a resolution. Current tools in use: ASP.NET entity framework Within a view, the following code is utilized: <button type="button" id="btnGraf ...

What causes the images in an overflow: hidden container to grow in size when hovered over?

When my images are hovered on, I want them to stay within the container without overflowing, which is why I have used overflow: hidden. However, the issue is that it causes the bottom of the container to expand and interfere with the content below. How can ...

Using JQuery for sending POST requests in JavaScript

I am currently facing an issue while trying to post JSON data to a server. I have managed to do it successfully using a certain method, which is as follows: <html> <head> <script src="http://code.jquery.com/jquery-latest.min.js">< ...

Looking for assistance in personalizing a Tab slider plugin with jQuery and CSS?

Hi there! I stumbled upon this post (http://stackoverflow.com/questions/7645702/tab-slide-out-using-jquery-need-more-than-one) while experimenting with a plugin. I'm curious if anyone here knows how to integrate this plugin into a menu. My idea is to ...

What is the method to close a JQuery dialog box without causing the close event to be triggered explicitly

Does anyone know how to close a JQuery Dialogue without triggering the close event? I have been trying to use $(this).dialog("close") within the dialogue function. Any assistance would be much appreciated. ...

What is the best way to rearrange three divs into two columns?

I am trying to rearrange the order of three divs in my markup layout. The desired layout is to display div one first, followed by three underneath it in the first column. Then, in the second column, I want to only show div two. My current CSS configuratio ...

Creating a three-dimensional network visualization in three.js

I'm currently working on creating a 3D graph using three.js Code: <html> <head> <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b7f6369747441567877746c79 ...

Avoid being redirected to another page after submitting the form, instead remain on the current page

Unable to understand why this functionality is not working the way it is supposed to... In my ASP.NET MVC5 form, I have two buttons - "Add New Product" which allows users to temporarily save product data (not to the database) and input information on anot ...

Adjust the height of a division dynamically

I have the following HTML snippet: <div id="container"> <div id="feedbackBox"></div> </div> The #feedbackBox div is initially not visible. To center the div, the CSS code is used: #container { position: absolute; widt ...