The HTML Image Source Attribute
In this article I explain the effects of changing the value of the HTML image source attribute.
Introduction
Did you know that you could change the value of an HTML image source attribute? If you do this, the image displayed at the browser would change. In this article I show you the effects of changing the value of the source attribute of the image tag.
You need basic knowledge in HTML, CSS and JavaScript in order to understand this article.
Note: If you cannot see the code or if you think anything is missing in this article (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
HTML Element
An HTML element is produced by either one or two tags. An HTML element is a component of the web page that you see at the browser. The image for example is produced by one tag. The Paragraph element on the other hand is produced by two tags (start and end tag).
The presentation of an element is produced by CSS. Presentation means the looks, position and size of an element.
The Image Element
A simple tag for the image element is:
With this image tag, the CSS presentation would be given by something like
img#ID {width:200px; height:200px}
In this particular presentation the position is not given, but the size is given.
Under this condition, if you change the value of the src attribute you will see a different image at the browser. This feature is very handy.
Changing the Source Attribute
The DOM and JavaScript enable you change the value of the image source attribute. The syntax is:
document.getElementById(ID).src = URL
The expression, "document.getElementById(ID)" returns a reference to the image object. "src" is the source property of the image object. ID and URL must each be in quotes. ID is the identifier of the image object.
Resulting Simplified Code
If it were not possible to change the value of the source attribute of an image, then to change an image you would have to change the whole tag. This would lead to a more complicated code.
Advantages of changing the value of the Source Attribute
- It leads to a simplified code.
- The presentation of the original image is maintained. When you change the value of the source attribute, the size and position of the previous image is maintained. This can be very useful in an image gallery where you have to present a large size image for different images.
Example
When you click the button of the following code, the displayed image would change. Try the code with two of your own images. The image and button is in an HTML Object element. The image is in another object element nested in the outer Object. The outer Object is floated right.
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Some text.

Change
Some text
The purpose of the outer Object element is just to hold the image and the button in one place. The inner Object separates the image from the button.
Animation
Now that it is possible to change the value of the source attribute of the image, you can have code that will be changing the image repeatedly and so produce some animation.
Well, I hope you now appreciate the fact that you can change the value of the source attribute of an image and do a lot with the feature.
Chrys
-
Fast Operating Image Gallery for New browsers
| By Chrys | in Programming
In this article, I show you how to design a fast operating image gallery, for new browsers....
-
A beginner's guide to HTML
| By aroshu123 | in Web Design
Hyper Text Markup Language (HTML) a script used to design simple websites. This article describes about HTML...
-
The HTML Object Archive Attribute
| By Chrys | in Programming
In this article I show you what you can do with the HTML Object archive attribute....
-
Fast Operating Image Gallery for All browsers
| By Chrys | in Programming
In this article, I show you how to design a fast operating image gallery, for all browsers....
-
XA Transaction - Solution for Transaction More Than One Database | By H4d1 | in Programming
Have you ever think that it's too difficult for making database transaction in two different places (or databases) ...
-
Javascript functions for : trim, right trim, left trim, no Apostrophe, is Empty , is Digit , VarChar To Number , is integer , check Is Zero , Get Que | By xxris | in Programming
Javascript functions for : trim, right trim, left trim, no Apostrophe, is Empty , is Digit , VarChar To Number , i...
-
How to access and use a Window's command line | By MaxwellPayne | in Programming
Learn about the Window's command line in DOS and how to use it....
-
How to Learn to Program Your Computer | By dsj8760 | in Programming
This article is about learning to program a computer. It is a general article giving tips on how to learn about pro...
-
Jailbroken iPhones get RickRolled | By explorer | in Programming
First iPhone worm, attacks via SSH and does the classic rick roll gag on the user....
-
Database N-to-N Relationships | By Chrys | in Programming
In this part of the tutorial we look at one-to-one, one-to-many and many-to-many relationships....
-
Database Associations Overview | By Chrys | in Programming
In this part of the series, we look at Database Associations Overview....
-
Database Table Data Types | By Chrys | in Programming
In this part of the series we look at database Data Types....
-
Database Table Keys | By Chrys | in Programming
In this part of the series we look at what is known as a key....
-
Database Design | By Chrys | in Programming
A database is a set of related tables. This is part 1, division 1 of a series I have on database....







No comments yet.