Forum Moderators: open

Message Too Old, No Replies

Javascript scope question

scope, domready

         

belfasttim

11:47 pm on Aug 13, 2009 (gmt 0)

10+ Year Member



Hi-- I am pretty sure this is a simple question but I've been banging my head against it and I'm not very experienced with Javascript.

I have a block of javascript in a Joomla site-- it's built into the view (in an MVC structured component) and it gets included into the template file, which is where most of the HTML is.

The Javascript I'm trying to fire is in the view block in a window.addEvent('domready',function(){}). So the code basically looks like:


window.addEvent('domready',function(){
function myFunction(){
alert("it works");
}
})

I want to call this function from an onChange event in the html template like this:


<input onBlur="myFunction()" type="text" class="inputbox" name="search" value="value">

I also tried


<input onBlur="myFunction();" type="text" class="inputbox" name="search" value="value">

and


<input onBlur="this.myFunction()" type="text" class="inputbox" name="search" value="value">

and


<input onBlur="window.myFunction()" type="text" class="inputbox" name="search" value="value">

but no matter what format I've tried Firebug tells me "myFunction is not defined" or "this.myFunction is not a function".

I know this is probably a scoping issue and a simple one at that, but I'm stumped. Thanks for any help.

Fotiman

3:20 am on Aug 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



window.addEvent is not a defined method, so you probably have that defined elsewhere. But, assuming that your addEvent function works correctly, then you could do the following:


window.addEvent('domready', function () {
myFunction = function () {
alert("it works");
}
});

This creates a global variable, myFunction, which holds a reference to your anonymous function.

belfasttim

4:15 am on Aug 14, 2009 (gmt 0)

10+ Year Member



that worked fotiman! thanks a lot.