1

I am trying to do a content slider controlled with a menu. Div elements will be placed in a wrapper and I am changing its position using script. Please check below code

jQuery(document).ready(function() {
    function goto(id){
        jQuery(".contentbox-wrapper").animate({"left": -(jQuery(id).position().left)}, 600);
    }
});   

and html where the function calling is,

<li><a href="#" onClick="goto('#homePage', this); return false">Home</a></li>

<li><a href="#" onClick="goto('#aboutPage', this); return false">About</a></li>

and the content divs is like

<div class="contentbox-wrapper">
    <div id="homePage" class="contentBox">
        content here
    </div>
    <div id="aboutPage" class="contentBox">
        content here
    </div>
</div>

the problem is, when i click on the menu items i am getting this error

" Uncaught ReferenceError: goto is not defined "

Please help..

5
  • 2
    The only way for an inline handler to find the function is if it's a global function. Yours is defined locally to the .ready() callback. Move it outside. There's no need to wait for the DOM to be ready to declare your function. Commented Dec 14, 2013 at 19:12
  • Also, the goto name may not work in order browsers, since in ECMAScript 3 it was a reserved word. Commented Dec 14, 2013 at 19:13
  • Why not bind to the click event in the jQuery DOM ready scope instead of using onClick? Commented Dec 14, 2013 at 19:14
  • @scrowler I'm a beginner in javascript.. don't know how to.. Thanks for your help.. Commented Dec 14, 2013 at 19:17
  • @cookiemonster rightnow I am not concerned with older browsers, atleast need to work in new browsers.. :-) thanks for the help.. Commented Dec 14, 2013 at 19:18

2 Answers 2

1

You should put the goto function outside the document ready event handler function. Also your function is taking a single parameter but you're using two every time you call it (not the case, just doesn't make much sense).

Sign up to request clarification or add additional context in comments.

Comments

0

You're passing goto two parameters when you reference it in the onclick attributes of the links but in its definition it only accepts a single param, the id.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.