WeBWorK Main Forum

hiding "Preview My Answers" button?

hiding "Preview My Answers" button?

by Andras Balogh -
Number of replies: 2

Is there a way to hide the "Preview My Answers" button maybe in selected courses?

Andras

In reply to Andras Balogh

Re: hiding "Preview My Answers" button?

by Glenn Rice -

There is a way that this can be done using the PGcourse.pl macro.  I do it for my courses.  Create the file PGcourse-hw.js, and place it in the html directory of the course.  The contents of the file are:

(() => {
	document.querySelectorAll('input[type=submit][name="previewAnswers"]').forEach((preview) => preview.remove());

	if (window.answerQuills) {
		for (const key in window.answerQuills) {
			window.answerQuills[key]?.removeEventListener('keydown', window.answerQuills[key].keydownHandler);
			window.answerQuills[key]?.addEventListener('keydown', (e) => {
				if (e.key === 'Enter') {
					const checkAnswersButton = document.getElementById('checkAnswers_id');
					if (checkAnswersButton) checkAnswersButton.click();
					else document.getElementById('submitAnswers_id')?.click();
				}
			});
		}
	}
})();
Next create a PGcourse.css file, and also place it in the html directory.  Its contents are:

input[type='submit'][name='previewAnswers'] {
	display: none !important;
}
Then create a PGcourse.pl file, and place it in the courses templates/macros directory. Its contents are:
 
sub _PGcourse_init {
	if (!$main::PG->{QUIZ_PREFIX}) {
		ADD_CSS_FILE("${main::htmlURL}PGcourse.css", 1);
		ADD_JS_FILE("${main::htmlURL}PGcourse-hw.js", 1, { defer => undef });
	}
	return;
}
The css file is not strictly necessary since the javascript removes the preview button, but without it the preview will usually flash on the screen before the javascript does that.

Note that this only works for regular assignments.  For tests different javascript is needed.  I also use that for my courses.  Let me know if you want that too.
In reply to Glenn Rice

Re: hiding "Preview My Answers" button?

by Andras Balogh -

This works, thank you. 

Unfortunately, a lot of local questions do not load PGcourse.pl for some reason, so they have to be edited.

I would appreciate seeing the solution for tests/quizzes.