Passing "img" of img.src as argument

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • enaz
    New Member
    • Feb 2009
    • 3

    Passing "img" of img.src as argument

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

    this is the function:

    Code:
    function mouseOver(c)
    {
    	alert(c);  <!-- just for debugging-->
    
    	if (c.src == "red.jpg")
    	{
    		document.c.src = "black.jpg";
    	}
    	else if (c.src == "black.jpg")
    	{
    		document.c.src = "yellow.jpg";
    	}
    	else if (c.src == "yellow.jpg")
    	{
    		document.c.src = "red.jpg";
    	}
    }
    this is how i made the images:

    Code:
    for (i = 0; i <= 27; i++)
    {
    	for (j = 0; j <= 55 ; j++)
    	{
    		document.write('<a href = "redbluegreen.html"> <img src ="red.jpg" border = "0" width = "20" height = "20" name = p onmouseover = "mouseOver(name)"> </a>');
    		document.p.name = namer(i,j);
    	}
    	document.write('<br />');
    }
    and heres the namer function:

    Code:
    function namer(i, j)
    {
    	return "r" + i + "c" + j;
    }
    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
    Last edited by Dormilich; Feb 5 '09, 07:14 AM. Reason: added [code] tags
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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).
    Code:
    if (this.src == "red.jpg") { ... }

    Comment

    • enaz
      New Member
      • Feb 2009
      • 3

      #3
      still not sure...

      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?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        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).
        Code:
        mouseOver(); 
        // "this" is undefined
        
        [I]element[/I].addEventListener("mouseover", mouseOver, false);
        // "this" = [I]element[/I]

        Comment

        • enaz
          New Member
          • Feb 2009
          • 3

          #5
          thank you very very much, i got it working!

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            always a pleasure to be of help

            Comment

            Working...