Registered User
Registered User

Reputation: 3699

How to dynamically get each element's innerHTML when accessing by class

How to dynamically get each element's innerHTML when accessing by class?

Here is an example: http://jsfiddle.net/DwDsL/

Upvotes: 0

Views: 1824

Answers (6)

Dhiraj
Dhiraj

Reputation: 33618

You might use .each

$('.btnItem').each(function(){
 // do your stuff using $(this)
});

Hope this helps

Upvotes: 4

Ram
Ram

Reputation: 144679

function g() {  
    $(".btnItem").each(function(){
       con = $(this).text();
       $("<div>" + con + "</div>").insertAfter(".btnItem")
    })

    $(".btnItem").remove();
}

http://jsfiddle.net/DwDsL/1/

Upvotes: 1

kei
kei

Reputation: 20481

$("span.btnItem").each(function(index) {
    $("<div>" + $(this).html() + "</div>").insertAfter($(this));
    $(this).remove();
});

demo

Upvotes: 1

thecodeparadox
thecodeparadox

Reputation: 87073

$('.btnItem').wrap('<div/>'); 

is enough

if you want to replace <span> with <div> remaining its html then try

$('.btnItem').replaceWith(function(a, html) {
   return '<div>' + html + '</div>';
});

Upvotes: 1

Chris Pratt
Chris Pratt

Reputation: 239260

Just use .wrap:

$(".btnItem").wrap('<div></div>');

Upvotes: 2

Pranay Rana
Pranay Rana

Reputation: 176896

use each function with class selector and html function to get innerhtml

$('.classname').each(function() {
    alert($(this).html());
});

Upvotes: 4

Related Questions