Rearrange the characters within the variable before inserting them after

I'm currently dealing with an external system that generates outdated code, however, I do have access to css and javascript. The specific issue at hand involves working with a table that displays each item on a separate row, sometimes resulting in up to 30 rows. My objective is to split these 'tr' elements into groups of 6 rows, with 5 items in each row.

The initial approach was to modify the style of each 'tr' element to display as table cells, thereby arranging them horizontally. Subsequently, I planned to assign a class to every 5th row and then insert closing and opening 'tbody' and 'table' tags after each 5th 'tr' with the designated class. This procedure was intended to create individual tables for every group of 5 'tr' elements, effectively forming rows of 5 items each.

While most of the setup works as expected, I encountered an issue with the .insertAfter method. The desired behavior was to insert '<\tbody><\table class='tableBreaker'><\tbody>' after every 5th 'tr', unfortunately, it produced unexpected results by reversing the order and outputting '<\table class="tableBreaker"><\tbody><\tbody>'.

I've tried searching for any relevant information regarding how jquery handles string manipulations within the .insertAfter function, but haven't found any clear solutions yet.

Table HTML:


<!-- Table HTML content goes here -->

CSS:


/* CSS styles go here */

jQuery:


// jQuery script goes here

Answer №1

A new function is needed to transfer the specified number of rows, indicated by rowCount, to new table(s) while transferring the old table classes to the new one(s).

function rowTransfer(rowCount) {
  var oldTable = $('#mainContent_rbl_zoneList'),
    holder = oldTable.parent().eq(0),
    rows = oldTable.find('tr');
  var classes = oldTable.prop("classList");
  var temptable = $('<table>');
  $.each(classes, function(index, value) {
    temptable.addClass(value);
  });
  for (var i = 0; i < rows.length; i = i + rowCount) {
    var newTable = temptable.clone(),
      tbody = $('<tbody>');
    rows.slice(i, i + 5).appendTo(tbody);
    newTable.append(tbody).appendTo(holder);
  }
  oldTable.remove();
}
rowTransfer(5);

It is important to note that this could cause issues if there are event handlers attached to the old table, which would need to be addressed.

Additionally, the new tables are applied to the end of the parent container of the old tables.

To adjust the CSS, you can also add:

temptable.addClass("rowcontainer");

and

.rowcontainer tr {
    display: table-cell;
}

Feel free to experiment with this sample: https://jsfiddle.net/n3nqespn/

Answer №2

When working with the insertAfter function to create new tables, your main goal should be to move the rows into these new tables.

One way to achieve this is demonstrated below:

var $container = $('#mainContent_rbl_zoneList'),
    $rows = $('#mainContent_rbl_zoneList tr');

for(var i=0; i < $rows.length; i = i+5 ){
   var $newTable = $('<table>'), $tbody = $('<tbody>');
   $rows.slice(i, i+5).appendto($tbody);
   $newTable.append($tbody).appendTo($container);      

}

Please note that this code snippet serves as a basic example and may need adjustments to account for row classes.

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

Guide to applying conditional styling to Material-UI TextField

After creating a custom MUI TextField, I implemented the following styling: const CustomDisableInput = styled(TextField)(() => ({ ".MuiInputBase-input.Mui-disabled": { WebkitTextFillColor: "#000", color: "#00 ...

HTML Tutorial: A Beginner's Guide to Invoking REST API

Hi there! I'm new to APIs and struggling to grasp the concept. The tutorials online seem more complex than necessary. Here's what I need help with: I want to create a basic webpage that allows me to search for information using the Pokemon API a ...

Attempting to embed an image within the datepicker input

After successfully implementing the datepicker to select a date, I decided to add a buttonImage option for opening the datepicker on image click. However, I encountered an issue when trying to place my calendar image inside my input element, floated to th ...

Select from the list

Can someone assist me with this Javascript code? I need to hide the following information and have it displayed only when a button is clicked. Any help would be greatly appreciated! $(document).ready(function(){ /* Function to toggle gadget visibili ...

How can I stack images on top of each other using Div, CSS, and ZIndex?

Currently, I am facing a challenge with overlapping 5 pictures. Despite reading numerous tutorials on CSS and div overlapping techniques, I still don't quite understand how to achieve it. The container where I want these images to overlap has a widt ...

Can login data be transferred from the index page to other pages?

Working on a school project involving a Weather info web page has been quite the journey. Most of it is complete, except for one issue that I can't seem to figure out. The index page with login and registration functions is connected to phpmyadmin/mys ...

How can I design DIVs to resemble a radio station or television schedule grid?

Check out the JSFiddle for my page with the source code included: https://jsfiddle.net/g0kw1Le8/ Here is a snippet of the source code: <TITLE>Hallam FM - Best Variety of Hits</TITLE> <style> /* CSS styles go here */ /* End of CSS */ & ...

Integrate a PHP form that includes a redirect feature and an optional opt-in box

I'm a beginner and I'm trying to enhance this code by adding some extra features. When the form is submitted, I want the page to redirect to an external URL like www.google.com The checkbox should be automatically checked and when the client re ...

The form embedded within the <tr> element seems to be malfunctioning

Hey there, I'm facing a little issue with my form that is nested inside a table content. It seems to not be working properly. Let me share the code snippet below: <table> <form name='editpo' method=POST action='js_buat_po.php? ...

Can you explain the significance of the "style.css?ver=1" tag?

Recently, I noticed that certain websites incorporate a CSS tag such as style.css?ver=1. Can you explain the significance of this? What exactly is the purpose of adding ?ver=1 to the CSS tag? Could you provide instructions on how to implement this in cod ...

Page redirection to a different URL after a successful AJAX call

I need assistance in registering a new user using an HTTP POST method with Ajax and Spring backend. I have successfully created a function to send JSON data to the controller and persist it in the database. However, I am facing an issue where after process ...

What is the best way to use shortcodes to display custom fields for post objects, specifically products, within WordPress posts

I'm in the process of displaying 'featured products' within my blog posts. These specific products need to be chosen using custom post objects on the backend for each individual post. I've outlined what I believe the PHP code should lo ...

How can the border of the select element be removed when it is active in select2

After examining the CSS code, I am still unable to locate the specific property that is being applied to the main element. I am currently customizing the select2 library to suit my needs. However, I am stuck in the CSS as I cannot determine which property ...

Can Spring Boot be set up to return JSON instead of HTML for InvalidTokenException when configuring OAuth2?

When testing my Spring Boot application, I realized that querying one of my REST endpoints with an invalid token using Postman resulted in a response content in HTML format instead of JSON. The endpoint correctly responded with 401 InvalidTokenException, b ...

Discovering the droppable container that a draggable element is positioned within

Currently, I am utilizing jQuery UI for drag and drop functionality. My main goal is to determine which droppable area a draggable element has been placed in. Can anyone offer assistance? Below is the code I am working with: $(".draggable").draggable({ ...

printing on the internet without the hassle of a print window appearing

Looking for a way to print in web without displaying the print dialog, I came across an interesting solution. However, it requires installing Download & install WebClientPrint for PHP. Is there a method to achieve this without the need for third-party ...

Ways to expand the width of mat-dialog-actions component in Angular 8

Is there a way to make the cancel and save buttons in the dialog window take up the entire available space? If anyone has any suggestions on how to achieve this, please let me know! ...

Adjusting the content and style of a div element upon clicking, and restoring the original settings when clicked once more

There is a div with the id #increase-text-weight that displays the text "INCREASE TEXT WEIGHT". Upon clicking on it, the font weight of another div with the id #post-content should be changed to font-weight: 500 and the text inside #increase-text-weight s ...

Trying to change the submit button value using jQuery is ineffective

I'm encountering an issue with changing the text displayed on a submit button using jQuery 1.4.5: $('.filter_watch').change(function() { $('#filter_submit').val("New text"); }); <script src="https://cdnjs.cloudflare.com ...

How feasible is it to customize the "ionic-img-viewer" npm package or replicate its image styling in Ionic 2?

I am wondering if it is possible to edit or add additional styles when my image is viewed using the "ionic-img-viewer" npm package. Alternatively, how can I apply the same styles as those on my original image tag? For example, here is my img tag with some ...