mardi 5 mai 2015

Copy elements from one cell to another cell in jquery

Using jquery, I'm trying to copy each "price" div after each "title" div.

<table>
  <tr>
    <td><div class="title">Item Title 1</div></td>
    <td><div class="price">$99.99</div></td>
  <tr>
  <tr>
    <td><div class="title">Item Title 2</div></td>
    <td><div class="price">$89.99</div></td>
  <tr>
  <tr>
    <td><div class="title">Item Title 3</div></td>
    <td><div class="price">$79.99</div></td>
  <tr>
</table>

So it would look like this:

<table>
  <tr>
    <td><div class="title">Item Title 1</div><div class="price">$99.99</div></td>
    <td><div class="price">$99.99</div></td>
  <tr>
  <tr>
    <td><div class="title">Item Title 2</div><div class="price">$89.99</div></td>
    <td><div class="price">$89.99</div></td>
  <tr>
  <tr>
    <td><div class="title">Item Title 3</div><div class="price">$79.99</div></td>
    <td><div class="price">$79.99</div></td>
  <tr>
</table>

I've tried using the jquery script:

$(".price").each(function(){
    var newLink = $(this).clone();
    $(newLink).insertAfter('.title');
});

That doesn't work - it just takes every price and appends it to each title (so you end up with 3 prices per title, in other words).

Does anyone have any ideas?

Aucun commentaire:

Enregistrer un commentaire