Having an issue where the Jquery clone function is only duplicating the content once. Looking to

I'm currently working on existing HTML table code. My goal is to copy the text from the 'th' header rows and paste them inside the corresponding td cells for each subsequent row. While I have successfully managed to copy the header row, it only repeats on the first row. How can I make it repeat on all following rows? Appreciate any help!

$(document).ready(function() {
  $('.head').each(function(i) {
    var $content = $('.new').eq(i);
    $(this).clone().prependTo($content);
  });
});
table,
tbody,
tr,
th,
td {
  border: 1px solid #000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<table id="table-container">
  <tbody>
    <tr class="header">
      <th class="head">Header 1</th>
      <th class="head">Header 2</th>
      <th class="head">Header 3</th>
      <th class="head">Header 4</th>
    </tr>
    <tr class="row>">
      <td class="new">Row 1</td>
      <td class="new">Row 2</td>
      <td class="new">Row 3</td>
      <td class="new">Row 4</td>
   </tr >
   <tr class="alt>">
      <td class="new">Row 1</td>
      <td class="new">Row 2</td>
      <td class="new">Row 3</td>
      <td class="new">Row 4</td>
    </tr>
  </tbody>
</table>
</div>

Answer №1

Iterate through all the rows in the table except for the ones with the .header class, and then for each row, loop through its cells and add a cloned version of the corresponding .head cell at the beginning.

$(document).ready(function() {
  $('tr:not(.header)').each(function() {
    $(this).find('.new').each(function(i) {
       $(this).prepend($('.head').eq(i).clone());
    });
  });
});
table,
tbody,
tr,
th,
td {
  border: 1px solid #000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<table id="table-container">
  <tbody>
    <tr class="header">
      <th class="head">Header 1</th>
      <th class="head">Header 2</th>
      <th class="head">Header 3</th>
      <th class="head">Header 4</th>
    </tr>
    <tr class="row>">
      <td class="new">Row 1</td>
      <td class="new">Row 2</td>
      <td class="new">Row 3</td>
      <td class="new">Row 4</td>
   </tr >
   <tr class="alt>">
      <td class="new">Row 1</td>
      <td class="new">Row 2</td>
      <td class="new">Row 3</td>
      <td class="new">Row 4</td>
    </tr>
  </tbody>
</table>
</div>

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

Issue adding dictionary value to an array in JavaScript

Let's analyze the code snippet below: var array = []; var obj = [{id: "xxxxx", name: "Friend name"}, {id: "xxxxx", name: "Friend name"}] for (x in obj){ array.push(x.name) } After running this code, the array ends up with the correct length but ...

Having trouble with reactjs and typescript? Getting the error message that says "Type 'string' is not assignable to type 'never'"?

When trying to setState with componentDidMount after an axios request is completed, you may encounter the error message Type 'string' is not assignable to type 'never'. Below is the code snippet: import * as React from 'react&apos ...

Tips for personalizing the react-select module

I'm struggling with customizing the appearance of the react-select component. The official documentation lists all the style attributes that can be modified, but I'm having trouble removing the blue borders around the text and container. https:/ ...

JQuery shake effect for dynamic text animation

Hey there! I need some help figuring out how to stop this shaking effect after it shakes three times. Do you think using set or clearInterval could do the trick? Any advice would be much appreciated! function interval() { $('#shake') ...

Error with website navigation menu

I am facing an issue where everything on my website works perfectly, except when I visit /treatments/manage_your_skin.php Once I navigate to that page and try to return to the homepage, it shows an error. This is because my website is searching for /treat ...

The filter is displaying incorrect categories

I am facing an issue with creating a work filter based on the last column which represents categories. When I select an option from the dropdown, I want to display only that specific category and hide the others. Currently, when I try clicking on an option ...

Unable to locate request object during post request

I've created a pug form with the following structure: extends layout block content h1 David A Hines h2 #{posts[0].title} p #{posts[0].body} div form(action='/insert_post',method='post') div(id='title_div' ...

Complete guide on modifying CSS with Selenium (Includes downloadable Source Code)

I am looking to switch the css styling of a website using Python and Selenium. My initial step was to retrieve the current CSS value. Now, I aim to alter this css value. The Python code output is as follows: 0px 0px 0px 270px How can I change it from 0 ...

Incorporate JSON data using jQuery's AJAX in MVC3

I need assistance with parsing the JSON data retrieved from a webservice through my controller. Currently, I am displaying the entire JSON string in a div as text. However, I only want to extract specific values such as "color" and "size". I am unsure of ...

What is the best way to retrieve the name of a Meteor package from within the

As I work on developing a package, I am looking for ways to dynamically utilize the package's name within the code. This is particularly important for logging purposes in my /log.js file. My main query is regarding how I can access the variable that ...

Python responding to an Ajax request using jQuery

Currently, I am experimenting with integrating a pre-built inline editor from the following source: https://github.com/wbotelhos/inplace Regrettably, the available support documentation is lacking and my expertise in Javascript, jQuery, or Ajax is limited ...

What is the best way to incorporate two banners on the Magento homepage alongside my flexslider?

I recently made the switch from using a camera.js slider on my website to a Flexslider. The reason for this change was that I couldn't make the camera.js slider clickable, and I wanted to eliminate the 'Click here' button. However, now that ...

How to dynamically add HTML content to a JSON string with the help of Javascript

I am working with a JSON object that includes various messages to be displayed on a webpage using Javascript. This JSON file is located in a separate directory and I have full control over it, being loaded asynchronously. There are specific cases where di ...

Is there a delay in Javascript identifying user existence when retrieving values from ajax?

I'm working on a script that checks if a username already exists before allowing a visitor to proceed. Here's a snippet of the code I'm using: EDIT: I've made some adjustments based on your feedback, but I'm still having trouble g ...

ETags for webpages generated on the server-side and equipped with a CSP nonce

I have successfully implemented ETags in my server-side-rendered React app using Node/Express, allowing for client-side caching. The HTML is generated with inlined fragments of render-blocking CSS and JS for faster first renders according to Google's ...

The output type of a function given an input

Essentially, I have successfully rendered a return type for my combined reducers using the following code: const rootReducer = combineReducers({ notes: notesReducer, categories: categoriesReducer, flyout: flyoutReducer // more reducers }); export ...

How to solve the issue of synchronizing data fetching from a json-server and displaying it in the UI with React?

While working with json-server to retrieve data and display it in the UI, I often face a recurring issue that leaves me puzzled. The process of fetching data is asynchronous, which means that setting the state after fetching the data takes some time. As a ...

Pause the video on YouTube when hiding a Bootstrap modal

Can anyone assist me in stopping a YouTube embedded video in bootstrap mode? I've tried, but nothing seems to work. Visit the page: <div class="modal fade bg-modal show" id="ModalVideofontano1" tabindex="-1" role="dialog" aria-labelledby="ModalV ...

The height set to 100% is causing issues with the padding-bottom property

Currently, I have a setup that includes: A div with width set to 100% and height set to 100% An SVG element inside the div with default width and height of 100% My issue is that when applying padding to the div, the left, right, and top padding seem to w ...

The functionality of HTML5 canvas image objects is not functioning as expected

I have been working on a function to retrieve an image object using HTML5 canvas, but I keep encountering an error alert (onerror event) function FetchImage() { var img = new Image(); img.src = "http://localhost/assets/images/loadedsprite.png"; ...