Kerberos
Kerberos

Reputation: 1256

create element on the fly and append a existing element to this, with using jquery

my existing code as follows

$('#'+ContainerId).find('img').each(function(){
    // ...
});

I want to crate a div element on the fly and append this existing img to this new created div element. Result must be like this which I want

<div style="text-align:center"><img src=".." /></div>

Upvotes: 1

Views: 194

Answers (2)

Andy E
Andy E

Reputation: 344793

You can use wrap(), and you don't even need each():

$('#'+ContainerId).find('img').wrap('<div style="text-align:center"></div>');

Example: http://jsfiddle.net/AndyE/WyXFb/

Upvotes: 0

jAndy
jAndy

Reputation: 236202

$('<div/>', {
    css:   {
        'text-align':   'center'
    }
}).append($('#'+ContainerId).find('img'));

Ref.: $(), .append()

Upvotes: 1

Related Questions