Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

modify all html of a certain class

Tags:

jquery

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

like image 449
Luca Romagnoli Avatar asked Nov 25 '09 13:11

Luca Romagnoli


People also ask

How do you customize a class in HTML?

HTML elements can belong to more than one class. To define multiple classes, separate the class names with a space, e.g. <div class="city main">. The element will be styled according to all the classes specified.

How do you change multiple classes in HTML?

Attribute Values To specify multiple classes, separate the class names with a space, e.g. <span class="left important">. This allows you to combine several CSS classes for one HTML element.

Can you assign the same class to multiple HTML elements?

Any number of elements may be assigned the same class name or names. Multiple class names must be separated by white space characters. Yes, just put a space between them.


3 Answers

$('.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.

like image 119
meder omuraliev Avatar answered Oct 23 '22 16:10

meder omuraliev


This should do it

$(".comment_date").each(function(i,e) {
  var x = $(e);
  x.html(x.attr("title"));
});
like image 43
jitter Avatar answered Oct 23 '22 15:10

jitter


try this:

$(".comment_date").each(function() {
    var cd = $(this);
    cd.html(cd.attr("title"));
});
like image 1
Ben Lesh Avatar answered Oct 23 '22 15:10

Ben Lesh