2

I have a table and a list:

<table>
  <tr>
       <td>Jack</td>
       <td class="date"></td>
  </tr>

  <tr>
       <td>David</td>
       <td class="date"></td>
  </tr>

  ...
</table>

<hr />

<span class="inline-date">2012</span>
<span class="inline-date">2013</span>

I want to append each span.inline-date to each td.date like this:

<table>
  <tr>
       <td>Jack</td>
       <td class="date"><span class="inline-date">2012</span></td>
  </tr>

  <tr>
       <td>David</td>
       <td class="date"><span class="inline-date">2013</span></td>
  </tr>

  ...
</table>

I tired:

  
  $("span.inline-date").appendTo('table td.date');
  
  

But I get double span in each td.

I miss something?

0

1 Answer 1

7

You can use each() like this;

var cells = $('table td.date');

$('span.inline-date').each(function (i) {
    cells.eq(i).append($(this));
});
Sign up to request clarification or add additional context in comments.

1 Comment

You might also want to use clone() to keep the original element in place. See this answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.