0
votes

I'm using mooTools and I need to call function TestA() on click event on a tag but that doesn't work. I know that I can attach event do a tag via $().addEvent(...) but I need use approach like bellow.

<script type='text/javascript'>
    window.addEvent('domready', function () {           
          function TestA(){}
      });

    function TestB(){}

</script>

<a href="#" onclick="TestA()">Test</a>
2

2 Answers

1
votes

Is there any reason you are defining your TestA function only when the DOM is ready? The reason TestA() is not working is because it is only defined within the scope of the function that is defined as the domready handler.

If you really do need to do it the way you're doing things then you'll need to scope the variable outside that function:

<script type='text/javascript'>

    var TestA;

    window.addEvent('domready', function () {           
        TestA = function(){}
    });

    function TestB(){}

</script>

<a href="#" onclick="TestA()">Test</a>
0
votes

You need to use the TestA = function (){} notation instead of function TestA(){} :

<script type='text/javascript'>
    window.addEvent('domready', function () {           
          TestA = function (){}
      });

    function TestB(){}

</script>

<a href="#" onclick="TestA()">Test</a>