JavaScript to show hidden question.

klaatu
klaatu Community Member Posts: 988
Is there a simpler JavaScript solution than below to display a hidden question (in this case qu39) object?

$("#qu39").css({
"visibility": "inherit"
});
$("#qu39").siblings().css({
"visibility": "inherit"
});
$("#qu39").siblings().children().css({
"visibility": "inherit"
});

Thanks.

 

Comments

  • pjackson2462
    pjackson2462 Community Member Posts: 69 ☆ Roadie ☆
    G'day Darrel,
    Assuming there is only one question object on the page you can use the following:
    var questionGrpObj = trivQuestionArray[0];
    var childCount = questionGrpObj.childArray.length;
    for(var i=0;i<childCount;i++){
    var jsObj = eval(questionGrpObj.childArray[i]);
    jsObj.actionShow();
    }

    As you can see there's no error checking.
    HTH
    Regards, Peter
  • klaatu
    klaatu Community Member Posts: 988
    Thanks, Peter but unfortunately there's not just one. I'm close to figuring it out. It is as simple as qu39.actionShow(); however I am trying to use a variable in place of the HTML Name. When I do, it throws an actionShow is not a function error. It's got to be something in the way I'm grabbing the name and assigning the variable but I need a day away from it. The weird thing is I console log or alert the variable I get qu39..

    It must be a type mis-match or something. It's usually something really simple, right?
  • timk
    timk Community Member Posts: 1,207 ♦ Idol ♦
    Hi Darrel,

    it can be done with:

    window[Varyourvariable.getValue()].actionShow();

    Tim
  • klaatu
    klaatu Community Member Posts: 988
    Thank you, Tim. While I am not using a Lectora variable, window[currentQuestion].actionShow(); works.  It needed the global reference. Thank you for that.

    Cool, I'm almost there. Now I'm trying to have Javascript trigger the processing of a question by calling the ProcessQuestion() function. It takes index and quest as parameters. I keep getting Uncaught ReferenceError: ProcessQuestion is not defined. If I put a hidden button on the page with an action to process any one of the questions then my code works. So it looks like I'm not initializing something correctly. I need to know how to call that function. Any idea's?

    Thanks.
  • timk
    timk Community Member Posts: 1,207 ♦ Idol ♦
    Hi Darrel,

    as far as I could find out, the processing of a question consists of two functions:

    1. ProcessQuestion()

    This function (I think) counts attempts, sets the question to "hasbeenprocessed = true" and runs the second function.

    This function is directly written to the source code of the page, it doesn't occur in any of the published JS-files or at least I couldn't find it. So if you don't have the "Process question" action on your page, it doesn't exist and can't be called from javascript.

    The question has two parameters: index = the index of this question in all questions on the page, so the first question in the page has the index = 0.

    2. checkQuestions()

    This function does the built-in feedback and attempts logic for all questions. It is written to the source code as well,  as it contains very question specific commands. It exists even if there is no Process Question action on the page.

    I have tested successfully to copy the "ProcessQuestion()" function to an external html object. The code is:
    function ProcessQuestion(index, quest) {
    if(!quest.hasBeenProcessed) {
    if( index >= 0 )
    currFeedbackIdx = index;
    checkQuestions( true, true, true );
    quest.hasBeenProcessed = true;
    }
    }

    It can be called from javascript with the index and the html name of the question. The feedback (On: Process question) was displayed correctly for both questions on my sample page.

    Of course now you must not have a "Process Question" action on the page ;-)

    Tim
  • klaatu
    klaatu Community Member Posts: 988
    Yes, success! Thank you, Tim. Adding the function, along with renaming a few of my variables to 'index' and 'quest',  did the trick.

    I'm going to have to print out a copy of published code (index.html) and either notate it or develop some kind of guide with all these tidbits. It would be nice to have them all in one place than to search the forums. Do you keep such a list or know if one exists?

    Again, thank you. You've saved me a lot of time, hunting and trial & error, especially with the global variable reference.

    Darrel