5

I have an image defined as following:

<div id="logo" class="exhibit-flowingFacet">
  <div class="wrapper">
    <img src="path/to/the/image/logo.png">
    </img>
  </div>
</div>

I want with a javascript get the image by its src and hide it.

function hideImage() { 
  if (document.getElementById() 
  {
    if (document.getElementById('logo').getElementsByClassName('wrapper').src=="path/to/the/image/logo.png")
    {
      document.style.visibility = 'hidden';
    }  
  }
};

hideImage();

But the code didn't work!! any idea?

2
  • you can add an id to the img attribute. and then with jquery you can use: $("#id").hide(); Commented Sep 11, 2013 at 15:01
  • the problem is I cannot edit the definition of the document. Commented Sep 11, 2013 at 16:15

5 Answers 5

3

With jQuery you could write

$('img[src="path/to/the/image/logo.png"]').hide();
Sign up to request clarification or add additional context in comments.

Comments

0

Try this script:

function hideImage() {
    var path = 'path/to/the/image/logo.png';
    var img = document.getElementById('logo').getElementsByTagName('img')[0];
    if (img && img.getAttribute('src').indexOf(path) > -1) {
        img.style.visibility = 'hidden';
    }
}

if (window.addEventListener) {
    window.addEventListener('load', hideImage, false);
} else if (window.attachEvent) { // for older IE8-6 compatibility
    window.attachEvent('onload', hideImage);
}

JSFiddle

4 Comments

thx I tried it works for seconds and then a java script appears:img is undefined PS.the image is not definied directly under the devision with the logo.have we to pass by the div with the className wrapper?
I am sorry i don't know why it doesn't work. Can you try the following modification? jsfiddle.net/ZC4yA/4 (the querySelector method works with IE8, it's still a bit more compatible than getElementsByClassName) or this jsfiddle.net/ZC4yA/3 (if you want also IE7 compatibility)
Thx a lot I used yours code to generate my exact case jsfiddle.net/amani1988/QCwVg/4 and it works :) but one I copy the code in my javascript file definied by <script> </script> it works no more! I think it requires an external javascript source that have to be added.Have you an idea?
Look at the source code of these two jsfiddles in your browser: jsfiddle.net/QCwVg/5/show or jsfiddle.net/QCwVg/6/show - the javascript should be executed after document is created (else .getElementById will return undefined as you said in your first comment), so either in the onload handler, or before the closing </body> tag.
0

You can try this:-

$('img[src="path/to/the/image/logo.png"]').css("display","none");

or try this:-

    this.style.display = "none";

Comments

0

getElementsByClassName('wrapper') is an array, which is why you have to access the src-attribute of the first element - at least in your case.

if (document.getElementById('logo').getElementsByClassName('wrapper')[0].src=="path/to/the/image/logo.png")

Comments

0

You have to make sure you have a link to jquery library in your header using this code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

You could diplay:none in css if you wanted it not to render on the screen, just pass the image source into the function

function hideImage(imageSrc) { 
$('img[src=" + imageSrc + "]').css("display","none");

}

and turn it back on using

$('img[src="path/to/the/image/logo.png"]').css("display","block");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.