Changing Image Src attribute using Title attribute... Possible..?? How??? With Javascript....

Hi,

Is it possible to use JS to alter the "src" attribute of an image tag using the "Title" attrubute to get the relevant tag.

So my full image tag is:

<img title="My Test Image" src="../Images/test.jpg" />

could be converted to:

<img title="My Test Image" src="../Images/test1000.jpg" />

using something like

Image = GetElementByTitle('My Test Image');
Image.Src = '../Images/test1000.jpg';

This image tag is autogenerated by something else hence the reason for not having the "ID" to play with.

Thanks in advance...

Sam
SamJollyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobinSoftware EngineerCommented:
Couldn't you enumerate all img tags in the document and pick the one you want to change?

E.g.:
function testImg()
{
   lst=document.getElementsByTagName('img');
   for (var i = 0; i < lst.length; i++) {
      if(lst[i].title=='My Test Image') lst[i].src='../Images/test1000.jpg';
   }
}

Open in new window

0
hieloCommented:
You don't even need document.getElementsByTagName(). The browser already has an images array/collection built in that references the images on the page:

<script>
for( var i=0;i<document.images.length; ++i)
{
  if( document.images[i].title=='My Test Image')
  {
     document.images[i].src='../Images/test.jpg';
      break;
  }
}
</script>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kadabaCommented:
perhaps clubbing both....

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
	<title>Images Test</title>
	<script type='text/javascript'>
	  // function that return an array of images with the title in question
	  document.getImagesByTitle = function(title) 
	  {
			var retnode = [];
			var imageTitle = new RegExp( '\\b' + title + '\\b' );
			var images = document.images;
			for( var i=0; i<images.length; ++i)
			{
			  if( imageTitle.test(title))
					retnode.push( images[i]);
			}
			return retnode;
	  };
	 
	 window.onload = function(){
		var imgArray = document.getImagesByTitle("EE");
		imgArray[0].src = "http://www.experts-exchange.com/xp/images/newNavLogo.png";
	 };
	</script>
	</head>
	<body>
		
		<img src="" title="EE"></img>
		
	</body>
</html>

Open in new window

0
SamJollyAuthor Commented:
thx all
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.