UserIsCorrupt
UserIsCorrupt

Reputation: 5025

Multiple parents with the same class with siblings with the same classes

<div class="example">
  <div class="test">Some text</div>
  <div class="whatever"></div>
</div>

<div class="example">
  <div class="test">Some more text</div>
  <div class="whatever"></div>
</div>

How can I make each .example's .whatever contain the contents of its .test?

For example, if the code were just this:

<div class="example">
  <div class="test">Some text</div>
  <div class="whatever"></div>
</div>

Then I could just do this:

$('.whatever').text($('.test').text());

But I have multiple elements with the class .whatever and multiple elements with the class .test, and each .test contains different text.

So in other words, how can I make every element with the class .whatever contain the text of the element .test, but only the element .test that is in the sample .example element?

Sorry for the confusing wording.

Upvotes: 2

Views: 125

Answers (2)

Naveed
Naveed

Reputation: 42093

Try:

$('.example').each(function() {
    $(this).find('.whatever').html( $(this).find('.test').html() );
});​

Tested Here

Upvotes: 3

VisioN
VisioN

Reputation: 145398

Try this:

$(".whatever").each(function() {
    $(this).text($(this).siblings(".test").text());
});

DEMO: http://jsfiddle.net/YXf5C/

Upvotes: 2

Related Questions