> I'm trying to mix jQuery with some pre-existing JavaScript/HTML, but > I'm having what appears to be scope issues. > > The code I'm working on already has a bunch of inline event handler > function calls in the HTML tags, and it'd be a ton of work to change > them all to the unobtrusive way via jQuery. Now when I wrap all the > pre-existing function definitions into the jQuery document instance > (i.e., $(function(){ ... }), I'm able to slip in some jQuery. > However, all the function definitions are now no longer available to > the inline event handler calls. > > Here's a simple demonstration. In this example the test() function is > not available when the div. > > <script> > $(function(){ > function test(){ console.log('hi')}}); > > </script> > > <div onclick="test()" style="background-color:red; width:300px">hi </div> > > Any ideas? I'm assuming many others have run into this predicament. > > Thanks for reading, > Eric P.
Don't put those function definitions inside your private scope. You need them to be global in order for the inline handlers to work. <script> function test(){ console.log('hi')}}); $(function(){ // put code here that needs to run when DOM is loaded }); </script>