Failed quiz question javascript with auto_nodetitle

Created on 20 April 2020, almost 5 years ago
Updated 11 September 2024, 4 months ago

I am using auto_nodetitle to programmatically generate node titles for quiz questions.

The module and function work just fine. However, there is a javascript error on the quiz question edit page because the form title field is absent. I think that Quiz has its own auto title feature for quiz questions and this may cause a small conflict. I prefer the auto_nodetitle because I can easily write a PHP snippet to generate the auto title that I prefer.

See solution below...

The code causing the issue is this, specifically the contents of the “if” condition:

if ($('#edit-title').val().length > 0) {
      $('#edit-body textarea:eq(1)').unbind('keyup', quizUpdateTitle);
}

The problem is that there is no element matching “#edit-title”. Initially, jQuery handles that just fine, returning an object of type “jQuery” with no elements.

Next, “.val()” is called, but since $(“edit-title”) returned no results, it’s working on an empty array. This also “works”, but returns the value “undefined”, which has no properties, instead of the string value of an element as it normally would.

Next, “length” is called, and this is where the problem happens, because it is operating on the “undefined” returned from “val()” which has no “length” property.

So the error message is a little confusing (at least in Firefox); it’s the return value of $(…).val() that causes the problem.

I’m assuming it’s trying to check if the title element is present and not whether it’s empty, in which case you can just check “length” of the jQuery directly, as follows:

if ($('#edit-title').length > 0) {….}
…instead of….
if ($('#edit-title').val().length > 0) {….}

If the intention is to check if it’s present but empty, then you can do that by checking if it’s present and empty in order, as follows:

if ($('#edit-title').length > 0 && $('#edit-title').val().length > 0) {….}

🐛 Bug report
Status

Closed: outdated

Version

5.6

Component

Code - Quiz core

Created by

🇺🇸United States webservant316

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸United States smustgrave

    With D7 EOL approaching triaging the D7 quiz queue and since this one has stalled going to close out. If issue is seen on 7.0.x please reopen

Production build 0.71.5 2024