JavaScript 'this' keyword

nightbandit

New Member
Messages
53
Reaction score
0
Points
0
I'm trying to link images with JavaScript. This is what I'm currently doing:

HTML:
<img src="filepath" alt="alt txt" onClick="window.open(this.src);" />
But I want to have a function to make it easier on myself. Here's what I've tried:

Code:
                    [IN HEAD SECTION]

                    function openImg(obj)
                    {
                            window.open(obj.src);
                    }

                    [IN IMG TAG]

                    onClick="openImg(this)"

But it's not working. I'm new to JavaScript so could someone please explain the problem to me, and maybe tell me how to fix it?

If you want to view the site here it is: http://craptastic.co.cc
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
The JS you posted looks fine. Check the error console and post relevant error messages here. What browser(s) are you having problems with? Always state the software environment.
 

nightbandit

New Member
Messages
53
Reaction score
0
Points
0
I'm using firefox. And the error I'm getting is openImg not defined.

Nevermind I had a typo in my script element, thanks anyays.
 
Last edited:
Top