views:

69

answers:

4

Suppose my code is like this:

<td class="apple">
<div class="worm">
text1
</div>
</td>

<td class="apple">
<div class="worm">
text2
</div>
</td>

<td class="apple">
<div class="worm">
text3
</div>
</td>

How can I loop through everything with "td class apple", and then grab the text of the div inside with id "worm", and then set each of the .attr() as that text?

Result:

<td class="apple" title="text1">
<div class="worm">
text1
</div>
</td>

<td class="apple" title="text2" >
<div class="worm">
text2
</div>
</td>

<td class="apple" title="text3">
<div class="worm">
text3
</div>
</td>

Thank you

+6  A: 
$('td.apple').each(function () {
    $(this).attr('title', $('div.worm', this).text());
});

Or this shorter version (supported as of jQuery 1.4):

$('td.apple').attr('title', function () {
    return $('div.worm', this).text();
});​
Marko Dumic
But $("div.worm")...will Jquery know that it's THAT class's worm?
TIMEX
I'm pretty sure you mean `td.apple` instead of `tr.apple`
Jeroen Pelgrims
@Jeroen: yes, a typo. thanks.
Marko Dumic
-1. Your attr line will pull in all div.worm regardless of whether it's child of the td or not.
SolutionYogi
@alex: Good point. Fixed (added context). Sorry, I'm too tired.@SolutionYogi: Sorry I hurt your feelings. I hope I helped alex ultimately.
Marko Dumic
Feelings? Are you kidding me? Anyway, I removed my downvote after you fixed your answer.
SolutionYogi
+1, I like the code supported by 1.4. Need to write more code with 1.4.
SolutionYogi
@SolutionYogi: Yes, beautiful piece of software! I was pleasantly surprised when it came out like that.
Marko Dumic
+1  A: 

This should do the trick.

//We will iterate through each td which has class of apple.
$('td.apple').each(
   function()
   {
       //'this' in the function refers to the current td.
       //we will try to find a div with class 'worm' inside this td.
       var title = $(this).find('div.worm').text();
       $(this).attr('title', title);
   }
);
SolutionYogi
A: 

I assume you mean td class="apple"

$("td.apple").each(function(){
  this.title=$(this).find("div").text();
});

If you're using me to do your school homework I will lock you in a phone booth full of bees.

Jethro Larson
I would prefer to use 'attr' method to ensure that the code will work cross browser.
SolutionYogi
@SolutionYogi: Do you know of a browser (that jQuery works in) that won't work with this construct (`this.title`)? I doubt that.
Marko Dumic
Marco, I stand corrected. I looked at the spec (http://www.w3.org/TR/html401/struct/global.html#h-7.4.3) and found that 'title' attribute is valid on ALL elements. For some reason, I was under the impression that it's only valid for certain elements.
SolutionYogi
+2  A: 

To add to the correct responses, I would recommend using children rather than find. Children is not recursive, any bit of optimization helps. Unless you need recursion through the TD's.

$("td.apple").each(function() {
    $(this).attr('title', $(this).children("div.worm").text());
});
Dustin Laine
Makes sense. You forgot to add the 'worm' class to your 'div' selector.
SolutionYogi
Sorry changed, also to use attr rather than title.
Dustin Laine