2

I would like to replace thumbs to large in following example using js

FROM:

<ul id="slide">
<li><img src="pathtoimg/thumbs/imagename.jpg" /></li>
<li><img src="pathtoimg/thumbs/imagename2.jpg" /></li>
</ul>

To :

<ul id="slide">
<li><img src="pathtoimg/large/imagename.jpg" /></li>
<li><img src="pathtoimg/large/imagename2.jpg" /></li>
</ul>

To achieve it I used following js code

 $(window).load(function(){
 var images = $("#slide li img");
 for(var i = 0; i < images.length; i++)
{
var img = images[i];
var src = img.src.replace("thumbs","large");
img.src = src;
}
  });       

The above code works fine in modern browsers but internet Explorer 7 & 8 returns stack overflow at line : 0 error. Is there any other way to replace the src of img in list without getting above error on ie?

Thanks in advance.

2 Answers 2

5

Try it like this

$(document).ready(function(){

    $("#slide > li > img").each(function(){ 
        var t = $(this);
        var src = t.attr('src');
        if(!src || typeof(src)!=='string') return;
        t.attr('src',src.replace('/thumbs/','/large/'));
    }); 

});
Sign up to request clarification or add additional context in comments.

4 Comments

I would suggest $(document).ready(function(){ $("#slide > li > img").each(function(){ var b=$(this),a=b.attr("src"); a&&"string"===typeof a&&b.attr("src",a.replace("/thumbs/","/large/")) }) });
Hi Thanks for your reply. I tried using your code on document.ready but it is still return stack overflow error in ie.
@DavidBélanger I tried using your code too.. It still shows me Stackoverflow error ...
@KuldeepDaftary Try this update: $(document).ready(function(){ $("#slide > li > img").each(function(){ var b=$(this),a=b.attr("src"); a&&"string"===typeof a&&b.attr("src",a.replace(/thumbs/g,"large")) }) });
1

Try this

$(window).load(function(){
$("#slide li img").each(function(){
$(this).attr("src",$(this).attr("src").replace("thumbs","large"));
});
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.