Mike Van Stan
Mike Van Stan

Reputation: 396

Can't select $(this).[elements of TD] and append it to another

I'm trying to grab the contents of the td="description" heading/title on the right of the image - in a table cell.

And add it underneath the img and continue the flow the same as before.

So far I have:

<script>
$('tr').each(function() {

 var desc = $(this).$('td.description').html() 
  $(this).$('td.image').append(desc);
  $('td.description').remove();


  });
 $("table#shopping-cart-items tr td.image").after($('<td id="clearSpace"></td>'));
 });


  </script>

IF I take "this" out - it correctly grabs it and appends it - but all the names are the same; so my use of $(this) I assume is breaking it i've also added the $('tr').each function?

Thank you for your time!

Upvotes: 0

Views: 44

Answers (2)

frogatto
frogatto

Reputation: 29285

this inside the each function refers to the currently being iterated item matched by $('tr'). In your case you would have two ways to go with:

  • $('td.description', this): matches with all td.description elements existing inside the this element.

  • $(this).find('td.description'): same as above.

Upvotes: 0

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

Try to use $(this) properly,

$('tr').each(function() {
  var desc = $('td.description', this).html() 
  $('td.image', this).append(desc);
  $('td.description', this).remove();
});

Your way of traversing is wrong. By default there is no function named $ under jquery wrapper object. That is why your code is breaking.

Upvotes: 2

Related Questions