David
David

Reputation: 1061

Set each elements's Html from its attribute

Instead of writing a loop like this:

$s.find("td.value").each(function() {
     $(this).html($(this).attr("data-value"));
});

Is there a way to do this in one shot ? Something like:

$s.find("td.value").html($(this).attr("data-value"));

Upvotes: 0

Views: 31

Answers (1)

PSL
PSL

Reputation: 123739

You can use the function argument of the html() to return the new html. But it doesn't make much difference since jquery internally loops through the items.

$s.find("td.value").html(function(){ //Has 2 arguments second one will be the current html representation of the element and first argument the index of the item in the collection
   return $(this).data("value");
});

Upvotes: 1

Related Questions