By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,688 Members | 1,936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,688 IT Pros & Developers. It's quick & easy.

Passing "img" of img.src as argument

P: 3
Ok, so, this is my first post, if there is any info i am leaving out please tell me

this is the function:

Expand|Select|Wrap|Line Numbers
  1. function mouseOver(c)
  2. {
  3.     alert(c);  <!-- just for debugging-->
  4.  
  5.     if (c.src == "red.jpg")
  6.     {
  7.         document.c.src = "black.jpg";
  8.     }
  9.     else if (c.src == "black.jpg")
  10.     {
  11.         document.c.src = "yellow.jpg";
  12.     }
  13.     else if (c.src == "yellow.jpg")
  14.     {
  15.         document.c.src = "red.jpg";
  16.     }
  17. }
this is how i made the images:

Expand|Select|Wrap|Line Numbers
  1. for (i = 0; i <= 27; i++)
  2. {
  3.     for (j = 0; j <= 55 ; j++)
  4.     {
  5.         document.write('<a href = "redbluegreen.html"> <img src ="red.jpg" border = "0" width = "20" height = "20" name = p onmouseover = "mouseOver(name)"> </a>');
  6.         document.p.name = namer(i,j);
  7.     }
  8.     document.write('<br />');
  9. }
and heres the namer function:

Expand|Select|Wrap|Line Numbers
  1. function namer(i, j)
  2. {
  3.     return "r" + i + "c" + j;
  4. }
now, what it does is create a 55x27 matrix of squares, when you mouseOver each square the idea is that it will change to the next color, I am passing the name of the image along to the mouseOver function. When I mouse over the image my alert(c) is telling me, correctly, the name of the square (i.e. r10c7) but the src of the image it tells me is unidentified when I modify the alert to alert(c.src). What I am thinking is that it is passing the name of the image to mouseOver as a string, but i'm not sure why this is a problem because I thought thats what the names of images always were. Any help would be greatly appreciated.

I know c++ reasonably well, but not javascript, I think I may be missing something as far as var goes i'm used to int double string and whatnot, also I have attached the full source if that is helpful
Attached Files
File Type: txt parlour.txt (1.1 KB, 281 views)
Feb 5 '09 #1
Share this Question
Share on Google+
5 Replies


Dormilich
Expert Mod 5K+
P: 8,639
a name is not a unique identifyer, better use id for that (along with getElementById()). probably the best is using the "this" keyword (then you wouldn't even require the name).
Expand|Select|Wrap|Line Numbers
  1. if (this.src == "red.jpg") { ... }
Feb 5 '09 #2

P: 3
OK, I now have the argument passed in correctly so that i can identify the src of the img, however, i'm not sure how to edit that src (i used getElementById(c)) to find the src) i assume i could use this.src to edit it, but in the scope of the function this.src is not defined, is there a way to make this = img or somehow pass this to the function?
Feb 5 '09 #3

Dormilich
Expert Mod 5K+
P: 8,639
the this variable is defined when the function is attached to the element (i.e. when the function becomes a method of the used element object).
Expand|Select|Wrap|Line Numbers
  1. mouseOver(); 
  2. // "this" is undefined
  3.  
  4. element.addEventListener("mouseover", mouseOver, false);
  5. // "this" = element
Feb 6 '09 #4

P: 3
thank you very very much, i got it working!
Feb 6 '09 #5

Dormilich
Expert Mod 5K+
P: 8,639
always a pleasure to be of help
Feb 6 '09 #6

Post your reply

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