5

My site uses jQuery 1.4.2. The problem is .replaceWith() does not work in IE6 & IE7 in jQuery 1.4.2. Is there an alternate method that is supported by IE6 & IE7 in jQuery 1.4.2?

The fiddle is here: http://jsfiddle.net/8CEwf/1/

I know, it may not seem jQuery is attached to it, but if you look in the HTML, the jQuery is there since jsFiddle does not offer version 1.4.2

HTML:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<img src="/v/vspfiles/templates/cyberfront/images/buttons/btn_addtocart_small.gif">
<input type="image" src="/v/vspfiles/templates/cyberfront/images/buttons/btn_go_gray.gif">
<img src="/v/vspfiles/templates/cyberfront/images/Bullet_MoreInfo.gif">

Script:

$(document).ready(function(){
$('img[src="/v/vspfiles/templates/cyberfront/images/buttons/btn_addtocart_small.gif"]').replaceWith('<br /><span id="blackbutton" class="mediumbutton" style="display:block;">Add to Cart</span>');
$('input[src="/v/vspfiles/templates/cyberfront/images/buttons/btn_go_gray.gif"]').replaceWith('<input type="submit" class="graybutton smallbutton" name="Go" alt="Go" value="Go" title="Go">');
$('img[src="/v/vspfiles/templates/cyberfront/images/Bullet_MoreInfo.gif"]').replaceWith('<span class="learnmore">Learn More</span>');
});
ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
henryaaron
  • 6,042
  • 20
  • 61
  • 80

3 Answers3

7
$("element").after("text to replace element with").remove();

Example.

We just select the element we want to replace, add some text after it, and remove it from the DOM.

Assuming that this is for an "Add To Cart" feature on your website, and that all the inputs will have similar src attributes, then why dont' you just add a class to them to make selection easier?

Purag
  • 16,941
  • 4
  • 54
  • 75
1

Here is another trick. You can empty the element before replace.

$(document).ready(function(){
     $(selector).empty().replaceWith('...');
});
Alex Kovanev
  • 1,858
  • 1
  • 16
  • 29
-4

This is a direct JS implementation of a PHP function that should serve your needs.

http://phpjs.org/functions/str_replace:527

TamDenholm
  • 143
  • 1
  • 4
  • 1
    This is a completely unnecessary measure to go to in order to achieve this simple, simple task. – Purag Dec 20 '11 at 02:39
  • @user1090389 Thats not PHP, its JS. Its a JS version of a PHP function. Purmou, Of course its unnecessary, but its still valid, was just suggesting an alternative solution. – TamDenholm Dec 20 '11 at 04:54
  • That's a much less powerful version of JavaScript's already existing String.replace :( – Benjamin Gruenbaum Apr 02 '13 at 11:39