3

I am trying to replace all broken image by JS. I use this code to replace all broken images by notfound.png image.

    $(document).ready(function() {
    $('img').attr('onError', 'this.src="notfound.png"');
});

Anyway, I would like to replace them by a text notice instead of image. I cannot find the proper way how to do it, thank you very much for your help. JS is not my cup of coffee :(

I would like to use this for the text part:

Text to to shown...

EDIT: OK, I have found this solution working fine, but doesnt accept CSS class

<script type="text/javascript">
$(document).ready(function() {
$('.post_body img').one('error', function() {
$(this).replaceWith('<div>Image not found (error 404)</div>').addClass('error404');
});
});
</script>

Anyway as I wrote CSS class is not added, so this solution is not complete :(

CSS will be:

.error404 {
   display: block;
   color: #667d99;
   font-weight: bold;
   font-size: 11px;
   border: 1px dotted;
   border-radius: 3px;
   padding: 5px;
   margin: 10px;
   background: #e7edf3;
   text-aling: center;
}

5 Answers 5

1

Ok, I think I have found a solution for you. I tried to use jQuery error function. This one helped me:

To replace all the missing images with another, you can update the src attribute inside the callback passed to .error(). Be sure that the replacement image exists; otherwise the error event will be triggered indefinitely.

In your example this would be the best:

$('img').each(function() {
    var img = $(this);

    img.error(function() {
        img.replaceWith('<div class="error404">Image not found (error 404)</div>');
    }).attr('src', img.attr('src'));
});

I also made a jsFiddle example for you, which is working great for me.

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

Comments

1

You can create a text node and append it to the parent of img, and optionally remove img if needed. This code goes inside the error handler for img

$('img').on('error', function(){
    $(this).parent().append($('<div>Broken image</div>'));
    $(this).remove();
})

Comments

0

If you really need to use javascript for this try

$(document).ready(function() {
    $('img').attr('alt', 'Alternative text');
});

But the same is achievable by barebone HTML

<img src="path/to/image.jpg" alt="Alternative text">

Comments

0

You can change the alt if an error is thrown just like you're doing with the image.

function imgMissing(image) {
image.onerror = "";
image.alt = "Image not Found";
return true;
}

The HTML:

    <img src="image.jpg" onerror="imgMissing(this);" >

Comments

0

EDIT: OK, I have found this solution working fine:

<script type="text/javascript">
$(document).ready(function() {
$('.post_body img').one('error', function() {
$(this).replaceWith('<div>Image not found (error 404)</div>');
});
});
</script>

Anyway one more think, I need to add CSS class "error404" for this "div", how to do that in JS? Thank you very much!

CSS will be:

.error404 {
   display: block;
   color: #667d99;
   font-weight: bold;
   font-size: 11px;
   border: 1px dotted;
   border-radius: 3px;
   padding: 5px;
   margin: 10px;
   background: #e7edf3;
}

3 Comments

So that code will be $(this).replaceWith('<div>Image not found (error 404)</div>').addClass('error404'); ???
$(this).replaceWith('<div>Image not found (error 404)</div>').addClass('error404');
look at my answer, I found a solution for you.

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.