2

please help me how to replace string url image from s120

s120/demo%2Bproduct%2Blipstik.jpg

to

s400/demo%2Bproduct%2Blipstik.jpg

//jQuery
$(document).ready(function(){
  $("#largeimage").click(function(){
        $(".container img").attr("src").replace("s120","s400");
    });
});

html

<div class="container center">
  <img src="https://2.bp.blogspot.com/-NwKHFcL48ZA/WJ2FANm2TXI/AAAAAAAAA6Y/hE-85KroGacBYy26mnEbkEGYFcnyWWv4gCLcB/s120/demo%2Bproduct%2Blipstik.jpg">
</div>
<p>
<button class="center" id="largeimage">Large Image to 400</button>
</p>

DEMO

2 Answers 2

2

You are not updating the attribute, do it using a callback with attr() method which holds the old value as the second argument.

$(document).ready(function() {
  $("#largeimage").click(function() {
    $(".container img").attr("src", function(i, src) {
      return src.replace("s120", "s400");
    });
  });
});
Sign up to request clarification or add additional context in comments.

Comments

0

Another approach - changing img src inside the attr function.

$(document).ready(function(){
  $("#largeimage").click(function(){
     var src = $(".container img").attr("src");
     $(".container img").attr("src", src.replace("s120","s400"));
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container center">
  <img src="https://2.bp.blogspot.com/-NwKHFcL48ZA/WJ2FANm2TXI/AAAAAAAAA6Y/hE-85KroGacBYy26mnEbkEGYFcnyWWv4gCLcB/s120/demo%2Bproduct%2Blipstik.jpg">
</div>
<p>
<button class="center" id="largeimage">Large Image to 400</button>
</p>

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.