473,473 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

element.src=variable?

martypants
2 New Member
What am I doing wrong here?

I'm trying to make a simple rollover that changes image from black/white to color. The images are displayed in b/w and all end in "_bw.jpg" and on mouseOver I want to change to color - thoses images all end in "_cr.jpg"

My javascript is:

Expand|Select|Wrap|Line Numbers
  1. function mouseOver(imgName)
  2. {
  3. //alert(imgName + "_cr.jpg");
  4. document.imgName.src = imgName + "_cr.jpg";
  5. alert(document.imgName.src);
  6. }
  7. function mouseOut(imgName)
  8. {
  9.  
  10. //alert(imgName + "_bw.jpg");
  11. document.imgName.src = imgName + "_bw.jpg";
  12. alert(document.imgName.src);
  13. }
the html side looks like this for an image:

Expand|Select|Wrap|Line Numbers
  1. <div align="center"><a href="Breakfast Menu.html"><img src="bfast_bw.jpg" name="bfast" width="399" height="600" alt="Breakfast" onmouseOver="mouseOver('bfast')" onmouseOut="mouseOut('bfast')" /></a>
The first alert, which I commented out, works fine. The second, which I would have expected to be identical, never shows up, which means its null
and the images never change.

Any clue as to how lame I am?

Thanks,
Marty
Mar 12 '09 #1
4 7273
Frinavale
9,735 Recognized Expert Moderator Expert
The parameter "imgName" is text.

Therefore the following won't work:
Expand|Select|Wrap|Line Numbers
  1. document.imgName.src = imgName + "_cr.jpg";
The reason is because document.imgName does not refer to an element.
Use the document.getElementByID() method to retrieve the img element, once you have this you can set the src of the img element.

For example:
Expand|Select|Wrap|Line Numbers
  1. function mouseOver(imgName)
  2.   var imageElement = document.getElementById(imgName);
  3.   imageElement.src = imgName + "_cr.jpg";
  4. alert(document.imgName.src);
  5. }
  6. function mouseOut(imgName)
  7.   var imageElement = document.getElementById(imgName);
  8.   imageElement.src = imgName + "_bw.jpg";
  9.   alert(document.imgName.src);
  10. }
Another way to do it is to pass the element directly into the method.
Then you don't have to use the getElementByID method.

For example:
Expand|Select|Wrap|Line Numbers
  1. function mouseOver(imageElement, imgName)
  2.   imageElement.src = imgName + "_cr.jpg";
  3.  alert(document.imgName.src);
  4. }
  5. function mouseOut(imageElement, imgName)
  6.   imageElement.src = imgName + "_bw.jpg";
  7.   alert(document.imgName.src);
  8. }

You would have to change your image button to pass the image element into the method. Yo use the "this" keyword to do so:
Expand|Select|Wrap|Line Numbers
  1. <img src="bfast_bw.jpg" name="bfast" width="399" height="600" alt="Breakfast" onmouseOver="mouseOver('bfast')" onmouseOut="mouseOut(this, 'bfast')" /></a>
Mar 12 '09 #2
Dormilich
8,658 Recognized Expert Moderator Expert
@Frinavale
you can also use the "this" keyword.
Expand|Select|Wrap|Line Numbers
  1. function mouseOver(imgName)
  2.   this.src = imgName + "_cr.jpg";
  3. }
it works even with Event Handlers
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("your_image_id").addEventListener("mouseover", mouseOver, false);
  2. // use addEvent() (=> google) for cross browser support
Mar 12 '09 #3
Frinavale
9,735 Recognized Expert Moderator Expert
Nice Dormilich :)

The solution is certainly cleaner than mine!
Mar 12 '09 #4
martypants
2 New Member
Works like a charm! Many thanks to you both!
Mar 12 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: M Wells | last post by:
Hi All, Is it possible to dynamically determine the current field element in a form? I ask because I have a 'tab' button on a form, and when a user clicks on it I need to be able to shift the...
2
by: James Marshall | last post by:
Does anyone know.... In JavaScript, is there any way to get a reference to a string variable (not an object), like Perl's "\" operator? I want to be able to compare two such references and know...
22
by: bmgz | last post by:
sorry for the stupid question, bu6 I haven't been able to find the answer anywhere.. consider this useless function which assigns an object to a var.. function(myParam){ var select1 =...
42
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
1
by: Sharon | last post by:
I need to write an XML document, that other users can work with to change values and to add elements. My problem is that for each element that me or any other user will add, should have some...
6
by: Dan | last post by:
Excuse me if i'm being a bit thick here, but is it possible to reference a server side variable within an embedded js source file. For example, my test.js file contains alert('<%=tmpVar%>'); ...
53
by: Aaron Gray | last post by:
Due to M$'s stupidity in not making DOMElements first class citizens the following will not work :- function isElement( o) { return o instanceof Element } It works for FF, Opera and Safari.
3
bilibytes
by: bilibytes | last post by:
Hi, I am trying to get the content of an element and reuse it elsewhere in the script lets say i have: <div id="content"> <ul name="subcontainer"> <li...
1
by: faultykid | last post by:
I would like to store a variable then call it back later. I have a variable on line 198 www = ''+this._ad.clickUrl+''; and on line 321 i try document.write(www);
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.