tags:

views:

39

answers:

3

i have this

<p class="comment_date" title="a">c</p>
<p class="comment_date" title="b">b</p>

i want put the title in the html of any element. so i try with this:

$(".comment_date").html($('.comment_date').attr("title"));

But it's wrong

how i can do it?

thanks

+2  A: 

This should do it

$(".comment_date").each(function(i,e) {
  var x = $(e);
  x.html(x.attr("title"));
});
jitter
wouldn't that reference the counter and not the element itself?
meder
Yup just a typo. Fixed
jitter
lmao... we posted at the same time... sorry. +1 for same answer. haha
blesh
+1  A: 
$('.comment_date').each(function() {
    $(this).html( $(this).attr('title') );
});

I think this should do it - let me know if that isn't what you're looking for.

It may be worth it to check if the title attribute length is >0. It's best to use .each for cases such as this, otherwise you're setting something to the combined value of multiple elements' values if you don't use .each.

meder
Didn't work for me. doesn't replaceWith() replace the whole tag and everything?
blesh
-1 As this actually removes the `<p>` tags instead of just setting value of the title as content
jitter
ah, appreciate the QA, thanks
meder
A: 
try this:

$(".comment_date").each(function() {
    var cd = $(this);
    cd.html(cd.attr("title"));
});
blesh