...

View Full Version : Changing the src attribute of an image



Bob Burns
07-16-2004, 01:59 PM
Hi,
I am trying to change the src attribute of an image, is there any way of doing so by passing in the id of the image to a function as follows

function ChangeImage(image_id)
{
document.getElementByID(image_id).src = "\images\hello.gif"
}//end of function

<img id="hello" src= "\images\bye.gif">

is that possible, I am aware of the document.images collection but I would prefer refering to the image using an id I provided, instead of looping through values.
I have another question to ask, I have two iframes (frame1,frame2) on a document, the links are on the left hand side on frame1 while the page's content is on frame2, I would like to change the iframes src attribute when the links on frame1 are clicked how can I do that?
Thanks
Bob

Willy Duitt
07-16-2004, 02:54 PM
Did you even try the code you posted?
Because from what I seen, it should work but I do not understand what you mean by looping thru the images collection. There is no need to loop if you pass the image name thru the function as you are with your id. This would have the added benifit of being backward compatable with browsers which do not support getElementById....

function ChangeImage(image_id)
{
document.images[image_id].src = "\images\hello.gif"
}//end of function

<img name="hello" src= "\images\bye.gif">

2) document.frames['frame2'].location.href = 'myNewSource.html';

.....Willy

Bob Burns
07-16-2004, 05:47 PM
Hi,
I am dynamically creating a table that contains links and images in my asp.net code as follows

sub page_load(sender, event_args)
{
Dim record_set As System.Data.SqlClient.SqlDataReader
record_set = command.ExecuteReader
Dim strBuilder As System.Text.StringBuilder

strBuilder.Append("<Table cellspacing= cellpadding>")
strBuilder.Append("<tr><td>")
strBuilder.Append("<a href= ' "& record_set.GetString("URL_LOCATION") &" ' onmouse = 'changeLinkColor( "& strLinkID&" )' id =' hello_name '>")

strBuilder.Append(" Google ")
strBuilder.Append("</a>")

literal_html_component_accessible_in_asp.InnerHtml = strBuilder.ToString()


}//end of page_load


my javascript code is
<script language="Javascript">

function changeLinkColor(link_ID)
{
document.getElementByID(link_ID).style.Color="ffff00"

}//end of function
</script>

for some bizzare reason when I pass in the argument link_ID into the javascript function is value isn't what its supposed to be, can anyone spot something wrong with my code syntax??
Thanks Bob

jamescover
07-17-2004, 03:17 AM
I am trying to change the src attribute of an image, is there any way of doing so by passing in the id of the image to a function as follows

function ChangeImage(image_id)
{
document.getElementByID(image_id).src = "\images\hello.gif"
}//end of function

<img id="hello" src= "\images\bye.gif">


What happend to the thread topic...?




<script>
<!--

function chImg(imgName){
document.images[imgName].src = "images/whatever2.gif"
}

//-->
</script>



<a href="javascript:chImg('whatever');void(0);"><img name="whatever" border="0" width="50" height="50" src="images/whatever1.gif" /></a>


or

<script>
<!--

function chImg(imgNum){
document.images[imgNum].src = "images/whatever2.gif"
}

//-->
</script>



<a href="javascript:chImg(0);void(0);"><img name="whatever" border="0" width="50" height="50" src="images/whatever1.gif" /></a>





-james



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum