I am almost done with upgrading my REGEXP question type from Moodle 1.9 through 2.0 to 2.1. I have a couple of very short javacript bits of code in my rendered.php file, which work fine, but I undertand that current moodle good practice requires all such scripts to be removed and placed in their own *.js files.
However, I haven't a clue how to achieve this.
Here is one example, which is uses a button to alternatively display/hide a list of acceptable answers to a REGEXP question, for the teacher in Preview mode.
if ($ispreview) {
$response = $question->get_correct_response();
$correctanswer = $response['answer'];
$show = get_string("showalternate", "qtype_regexp");
$hide = get_string("hidealternate", "qtype_regexp");
echo("<input type=\"button\" value=\"$show\" onclick=\"showdiv('allanswers',this)\" />");
echo ("<script>
function showdiv(id,thisbutton) {
if (document.getElementById(id).style.display == 'none') {
document.getElementById(id).style.display = 'block'
thisbutton.value = '".$hide."'
} else {
document.getElementById(id).style.display = 'none'
thisbutton.value = '".$show."'
}
return;
}
</script>");
}
Help on how to move this away to a script.js file would be appreciated.
Joseph