2

I'm looking to replace an image with a class to a div with text inside it, how can I do that?

<script>
$('img.vCSS_img_history_off').replaceWith('<div id="button">Turn History Off</div>');
</script>

I know that code is screwed up, but it's just to paint a picture of what I'm looking for, any suggestions?

Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
henryaaron
  • 6,042
  • 20
  • 61
  • 80

3 Answers3

0

An alternative could be to use this but it will erase other parent's child (as @Vide Simas said):

$(".myImage").parent().html('<div>hello</div>');

What you wrote looks like good, i've written a jsfiddle using replaceWidth: http://jsfiddle.net/ydZg5/

The error must be somewhere else

phemios
  • 664
  • 4
  • 9
0

The code below will replace an image with class name "myImageClassName" with a div containing the text "This is my div text":

$('img.myImageClassName').replaceWith('<div>This is my div text</div>');

but yeah, I gotta agree with the others.. your code looks fine.

dan
  • 81
  • 4
  • Re-writing the question is not considered as an answer. Anyway, the solution was wrote down in the question comments 5min ago. – Pierre Dec 11 '11 at 23:22
0

You need to make sure the DOM is ready before you go searching for elements in it:

$(document).ready(function(){
    $('img.vCSS_img_history_off').replaceWith('<div id="button">Turn History Off</div>');
});

Alternatively, you can add whatever you need to after the image and then remove it:

$(document).ready(function(){
    $("img").after("<div>Inserted div</div>").remove();
});

Example.

More on .after() here and more on .remove() here.

Purag
  • 16,941
  • 4
  • 54
  • 75