0

How can I replace images by others with javascript please ?

I've tried but something is wrong

 if ($(".grimm")) {
   $('.ch').
     html(
       $('.ch').
         html().
         replace('http://icons.iconarchive.com/icons/graphicrating/koloria/32/Mail-icon.png',
                 'http://icons.iconarchive.com/icons/graphicrating/koloria/32/Mails-icon.png')
   );
 }
<div class="grimm">
  <div class="ch">
    <img src="http://icons.iconarchive.com/icons/graphicrating/koloria/32/Mail-icon.png" />
  </div>
</div>

1
  • What is the "something" that "is wrong"? Commented Feb 25, 2015 at 23:18

1 Answer 1

3

Make sure to include jQuery, then your fiddle works fine: http://jsfiddle.net/dm9xkmkd/1/

But you can also handle this in one step without involving replace() or rewriting large chunks of HTML:

$('.grimm .ch img[src="http://icons.iconarchive.com/icons/graphicrating/koloria/32/Mail-icon.png"]').
  attr('src', 'http://icons.iconarchive.com/icons/graphicrating/koloria/32/Mails-icon.png');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div class="grimm">
  <div class="ch">
    <img src="http://icons.iconarchive.com/icons/graphicrating/koloria/32/Mail-icon.png" />
  </div>
</div>

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.