View Full Version : onclick syntax

12-05-2005, 06:20 AM

I have an infuriating 'object expected' error, that I just cant find. Perhaps a new perspective could shed light on it. I have two images (left and right arrows) to which I have attached onclick commands. But when I click on them, I get the 'object expected' error on the following lines:

<img src="./?f=101103" onclick="moveSlideLeft(); return true;"/>
<img src="./?f=101104" onclick="moveSlideRight(); return true;"/>

The onclick commands should jump to the commands below. I put in some alerts at this point. Its not even reaching these functions, so I dont think the immeadiate error lies within updateSlide(), so I wont crowd you with the code here. I guess that the problem must be how I'm calling the moveSlideLeft/Right functions from the onclick property.

function moveSlideLeft(){

function moveSlideRight(){

12-05-2005, 01:10 PM
Should work... Anyway there is no need to return true. And probably you need to specify the type of your script code

<script type="text/javascript">

Wouldn't be normal a straight trigger?:

<img src="./?f=101103" onclick="updateSlide(-1)"/>
<img src="./?f=101104" onclick="updateSlide(1)"/>

12-06-2005, 05:56 AM
The only reason I had the two wrapping functions was in case the -1 and 1 weren't being parsed properly or something... I had what you've suggested earlier. I expect both ways work and the problem lies elsewhere.

Maybe its got to do with where my script is in the document... I'm working within a Content Management System called MySource. I have a script file which is referenced in the header of each page, but I didnt put this script in it (the one for updateSlide) because it only applies to one page. I figured it made more sense to locate the function near the object that it relates to, rather than being lost in a huge javascript file.

Is that a problem?


12-06-2005, 09:19 AM
I figured it made more sense to locate the function near the object that it relates to, rather than being lost in a huge javascript file.

Nope, this is not the problem, except maybe if the code is far away bellow the element and the event try to trigger a function which was not loaded, if the action is quickly enough. Most of the time javascript codes are to be placed in the HEAD of the document, for this reason.

But it looks like you might encounter the same problem which apperas when trying to insert a javascript code in a .tpl page (using Smarty, for instance). In that case, the javscript code must be marked as a "literal"

<script type="text/javascript">
...some code here...

I have never worked with MySource, so that I just presumed that it might be a similar problem.

12-07-2005, 06:01 AM
The {literal} thing doesnt work, it just prints it onto the page.

I checked it in FireFox and found that the javascript console says that the problem is that moveSlideLeft and moveSlideRight are not defined...

But the script that does define them is above where I state the mouseover property(!)

Should I use a different syntax to define the functions?


12-07-2005, 06:08 AM
Opera gives the following message:

Event thread: click
name: ReferenceError
message: Statement on line 1: Reference to undefined variable: moveSlideLeft
Line 1 of script