0

I'm currently have this input for my img tag:

  <div id="myDiv">
      <img src="/assets/images/upload/test1.jpg" />
      <img src="/assets/images/upload/test2.jpg" />
      <img src="/assets/images/upload/test3.jpg" />
  </div>

How I can use jquery to add image path like /newfolder/ to all image tags in myDiv using jquery? I found that jquery can alter the image src...for example

$('#myDiv img').attr('src','/newfolder/assets/images/upload/test.jpg')

Now..how I can continuously find other images in myDiv and add pre-path like /newfolder/ to all image src in myDiv?

1 Answer 1

1
var picRoot="/newfolder";

$('#myDiv img').each(function(){
    if ( $(this).attr('src').indexOf(picRoot)!== 0){
        $(this).attr('src', picRoot + $(this).attr('src'));
    }
});

Call it, if your content changes.

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

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.