1

Nothing major, but I need to know if there is a way for me to change html elements with containing properties to something else with jquery.

What I actually want to do is to change the following piece of markup after a page load:

<div class="stretchMe" data-stretch="@Url.Content("Images/img1.jpg")"></div>

to

<img class="stretchMe" src="@Url.Content("Images/img1.jpg")" />

How to do something like this?

1 Answers1

1

Try with .replaceWith()

html

<div class="stretchMe" data-stretch="@Url.Content(Images/img1.jpg)"></div>

JS

$("div.stretchMe").each(function () {

    var class1 = $(this).attr("class");

    var src = $(this).data("stretch");

    $(this).replaceWith("<img class=" + class1 + " src='" + src + "' />");
});

DEMO

OP

<img class="stretchMe" src="@Url.Content(Images/img1.jpg)">
Balachandran
  • 9,567
  • 1
  • 16
  • 26