HD Quiz

Need to send invoices?
Check out my new WordPress plugin HDInvoice
Limited time launch sale

Case-sensitive option?

Published: June 28, 2023
Support status: closed

Hello,
I’d like to have text-based answers that are case-sensitive, but I know it mentions in the plugin that answers are case-insensitive.
Is there any setting or code I can add to a specific question to make answers case-sensitive?

Thanks!

thread author: Rale S

Hi Rale,
sorry for the delayed response.

There is a way to disable it, but it would be a global change that affects all of your questions. It can’t be enabled or disabled on a per-question basis – well, not unless you are willing /able to do some modifications.

If you edit ./hd-quiz/includes/js/hdq_script.js then on lines 391 and 396 you will see references to toLocaleUpperCase().

This is the function that takes both the list of accepted answers and the quiz taker’s answers and makes them all uppercase to normalize them.

You can simply remove the toLocaleUpperCase() from both lines and now the text based answer questions will all be case-insensitive.

But how to only affect certain questions? First you need to find the ID of the question. The easiest way to do this is to use the inspector tool of your browser. (right-click a question and inspect element). The data-id is the ID of the question. See screenshot example below.

View post on imgur.com

Now that you have the ID, let’s take a look at ./hd-quiz/includes/js/hdq_script.js again and this time, on line 390 you can see the that we are passing a argument el. el is that I call “elements”, which in this case, is the actually HTML node of the question.

So we can do the following to get the current ID of the current question we are marking.

let question_id = el.getAttribute("data-id");

How does this help?

Well, if you know the ID of the question that you want to be case-insensitive, and we now know which question we are marking…

            const question_id = parseInt(el.getAttribute("data-id"));
            let case_insesitive_ids = [118, 119, 223]; // enter the ID of each question you want to be insensitve, comma separated
            let value = el.value.toLocaleUpperCase().trim();
            if (case_insesitive_ids.includes(question_id)) {
                value = el.value.trim();
            }

            let answers = el.getAttribute("data-answers");
            answers = decodeURIComponent(answers);
            answers = JSON.parse(answers);
            if (!case_insesitive_ids.includes(question_id)) {
                for (let i = 0; i < answers.length; i++) {
                    answers[i] = answers[i].toLocaleUpperCase();
                }
            }

Assuming I didn't make a dumb mistake, that above code should get you what you are looking for. Hopefully, even if you are not able/willing to make these edits, someone else might find it useful.

30 June 2023 — 12:38 support admin - Dylan

If you are enjoying HD Quiz please leave a review here on the official WordPress.org page. HD Quiz is developed by me, just some dude, and is supported and available for free. It may seem dumb, but truly nothing makes me happier than knowing that people are using and loving HD Quiz and my hard work.

This thread has either been marked as complete or has been automatically closed due to inactivity.