Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a hidden <img> in JavaScript?

How can you make a simple tag like <img src="a.gif"> hidden programmatically using JavaScript?

like image 370
joe Avatar asked Jun 15 '09 15:06

joe


People also ask

How do I hide an image in HTML?

The trick to hiding any element on your web page is to insert either a " display: none; " or " visibility: hidden; " rule for that element. The " display: none; " rule not only hides the element, but also removes it from the document flow.

How do I hide a picture with a dom?

Style display property is used to hide and show the content of HTML DOM by accessing the DOM element using JavaScript/jQuery. To hide an element, set the style display property to “none”.

What is hide () in JavaScript?

The hide() method hides the selected elements. Tip: This is similar to the CSS property display:none. Note: Hidden elements will not be displayed at all (no longer affects the layout of the page). Tip: To show hidden elements, look at the show() method.


2 Answers

I'm not sure I understand your question. But there are two approaches to making the image invisible...

Pure HTML

<img src="a.gif" style="display: none;" />

Or...

HTML + Javascript

<script type="text/javascript">
document.getElementById("myImage").style.display = "none";
</script>

<img id="myImage" src="a.gif" />
like image 167
Steve Wortham Avatar answered Nov 03 '22 23:11

Steve Wortham


How about

<img style="display: none;" src="a.gif">

That will disable the display completely, and not leave a placeholder

like image 37
Brian Agnew Avatar answered Nov 03 '22 22:11

Brian Agnew