src without image does not disappear

Asked

Viewed 196 times

1

I am using the JSON database and I have a problem: when there is an image in JSON, instead of the tag <img> does not appear, it appears as if the image of that error:

inserir a descrição da imagem aqui

Is there any way when the src not found in JSON, tag img not show up?

Code:

var image3: obj.imagem3;
var image: obj.imagem;

$("#imagemareas3").attr("src", this.image3);
$("#imagemareas").attr("src", this.image);


<img class="imageminfo" id="imagemareas" src="">
<img class="imageminfo" id="imagemareas3" src="">    

JSON:

[    
    {
        "titulo": "Exemplo",
        "imagem": "",
        "imagem3": "",
        "id": 0
    }
]
  • 2

    Yes, do not insert the image on the page\(ツ)

  • @Andersoncarloswoss but I need it because in other json arrays there already has an image but in case it doesn’t have an image it disappears

  • 3

    Exactly. It makes no sense for you to insert the element <img> on the page if the src will be empty.

  • 2

    If you’re interested, did you know that you can style and customize an image that doesn’t load? It does not need to appear so you can treat the image that fails to load, read more here https://answall.com/questions/321138/comort-do-um-stilo-para-broken imagem-quando-imagem-n%C3%A3o-load

1 answer

7


The ideal would be the TAG img nor exist in the DOM and not hide the same. As I noticed that makes use of jQuery, I leave below some options with the framework and also an option with CSS (that would not recommend, since you do not want the image in the DOM).

Option 1 (recommended)

You could add the images in the DOM, only if the address was not empty. Below is an example:

html

<!-- div onde as imagens vão ser renderizadas -->
<div id="images"></div>

javascript

// código javascript para adicionar elas na div
if(obj.imagem)
   $('#images').append('<img src="'+obj.imagem+'" />');
if(obj.imagem3)
   $('#images').append('<img src="'+obj.imagem3+'" />');

Option 2

You can also remove the DOM image if the property is not filled in. Below is an example:

this.image3 ? $("#imagemareas3").attr("src", this.image3) : $("#imagemareas3").remove();
this.image ? $("#imagemareas").attr("src", this.image) : $("#imagemareas").remove();

Option 3

But in case you insist on maintaining TAG in the DOM you can make use of the CSS to hide the image (remember that it is only an outline solution and do not recommend the use), follows example:

img[src=""],img[src="null"], img[src="undefined"] {
     display: none;
}
  • It’s right as soon as I can give you the right!

  • 1

    @Davidmv combined, I updated the response with a second option.

  • 1

    The "second option" should be the first (and only) option. I would recommend first teaching to do right, then doing gambiarra (optional).

  • 1

    @Andersoncarloswoss you are correct, I will update the answer!

  • 1

    @Andersoncarloswoss thanks for the comment, updated reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.