Aly_Elgahy
Aly_Elgahy

Reputation: 189

Image change style when only have source

I want to change image height only when its src is not empty with JQuery or JavaScript , i have an image that have fixed height and in chrome they always keep blank place for it even when there is no source for it i want show this height only when image have source

Upvotes: 0

Views: 63

Answers (3)

Naele
Naele

Reputation: 530

You can use

  img[src=""] {
     display: none;
  }

You can change the height or not display it..

Upvotes: 2

novs12
novs12

Reputation: 667

If you want to use javascript instead of CSS, you can try calling this function whenever you want to check if the image has a source / display the image.

Set an ID for your image and then:

function check()
  {
    if (document.getElementById("yourimgidhere").src == "")
      {
        document.getElementById("yourimgidhere").style.display = "none";
      }
      else
      {
        document.getElementById("yourimgidhere").style.display = "inline";
      }
  }

The CSS method is far more effective and is the proper way of doing it as other people have answered, but this should work too.

Upvotes: 2

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123397

jQuery is not necessary, just use css to define a zero height for images with an empty src attribute:

img[src=""] {
    height: 0
}

Upvotes: 2

Related Questions