0

How can I add javascript to MoodleForm in Moodle v1.9.

I tried the following code:

$mform->addElement('static', null, 
    '<script type="text/javascript">
    //<![CDATA[
        $(\'input#id_userfile\').change(function() {

            var filePath = $(this).val();

            if (filePath.indexOf(\'aaa\') > 0)
                $(\'select#id_roleassign\').val(\'11\');
            else if (filePath.indexOf(\'bbb\') > 0)
                $(\'select#id_roleassign\').val(\'22\');
            else
                $(\'select#id_roleassign\').val(\'\');
        });
    //]]>
    </script>');

But it doesn't seem get javascript into the page source.

Any idea how to add javascript to the MoodleForm code?

Thanks for your help!

klor
  • 1,237
  • 4
  • 12
  • 37

1 Answers1

0

Have you tried:

$mform->addElement('html', '[insert any html you want here]');

(But, please stop using Moodle 1.9 - it has been unsupported for any form of security fixes for over 5 years now and is basically very old and out of date).

davosmith
  • 6,037
  • 2
  • 14
  • 23
  • Thanks, I will try. Upgrade is under way, but needs a lot work to upgrade the custom developments to Moodle 3.x. – klor Oct 13 '17 at 20:43