asd
asd

Reputation:

img tag convert into div tag by using jQuery

I want to change all img tag of a page into div tag by using jQuery. ex.

<img src="http://www.xyz.com/img.png" alt="this text i want in div" />

by using jQuery it converts into

<div>this text i want in div</div>

How we can do this by using jQuery?

Upvotes: 0

Views: 2923

Answers (1)

JohnP
JohnP

Reputation: 50019

You could use replaceWith

Here's a fiddle : http://jsfiddle.net/Am75c/

<img src="http://www.xyz.com/img.png" alt="this text i want in div1" />
<img src="http://www.xyz.com/img.png" alt="this text i want in div2" />
<img src="http://www.xyz.com/img.png" alt="this text i want in div3" />

$('img').each(function(){
    $div = $('<div>').html($(this).attr('alt'));
    $(this).replaceWith($div);
});

Upvotes: 1

Related Questions