new row appears only when the previous row has been filled

More
2 years 10 months ago - 2 years 10 months ago #111587 by qdrop
qdrop created the topic: new row appears only when the previous row has been filled
Dear forum.

I intend to implement javascript that allows to hide empty rows of an "multiple short texts" array question.

Therefore, I've put the following code at the beginning of the question:
<script type="text/javascript" charset="utf-8">
//Function to only display a new row if there is an answer in the previous row
//NOTICE: Rows which are reset to "No answer" will not be hidden
//NOTICE: This scipt was written based on LimeSurvey 2.00+ build 131107
//NOTICE: It only works for single-choice arrays (e.g. 5-point scale array) or multiple short texts
//BEGIN
$(document).ready( function() {
    //SGQ code of the question to apply this to
    var quest = "{SGQ}";
    //A(nswer) code of the first row
    var first = "1";
 
    //hide all rows except the first
    $("tr[id^='javatbd" + quest + "']").css("display","none");
    $("tr[id='javatbd" + quest + first + "']").css("display","table-row");
 
    //display rows if previous is answered
    $("[name^='" + quest + "']").change(function() {
        if(this.value.trim().length >= 1)
            $("tr[id='javatbd" + this.name + "']").next().css("display","table-row");
    });
});
//END
 
</script>
Now, the part that hides every row except the first one is working flawlessly. Unfortunately as soon as I'm inserting something in the first row, no additional row is showing up (as intended)...

Could you please tell me what I'm doing wrong? I think this functionality would be great to implement in Limesurvey anyways since it makes questions / surveys much more comprehensible.

Rgds

qdrop
Last Edit: 2 years 10 months ago by qdrop. Reason: removing typos

Please Log in to join the conversation.

More
2 years 10 months ago #111627 by qdrop
qdrop replied the topic: new row appears only when the previous row has been filled
I found part of the error:

The variables "quest + first" are the ID of a <tr> (that's why the hiding is working), "this.name" is the ID of a cell...(that's why the unhiding is NOT working).

But how to fix that? I'm no javascript-programmer...

Please Log in to join the conversation.

More
2 years 10 months ago #111639 by qdrop
qdrop replied the topic: new row appears only when the previous row has been filled
The following code works:
<script type="text/javascript" charset="utf-8">
//Function to only display a new row if there is an answer in the previous row
//NOTICE: Rows which are reset to "No answer" will not be hidden
//NOTICE: This scipt was written based on LimeSurvey 2.00+ build 131107
//NOTICE: It only works for single-choice arrays (e.g. 5-point scale array) or multiple short texts
//BEGIN
$(document).ready( function() {
    //SGQ code of the question to apply this to
    var quest = "{SGQ}";
    //A(nswer) code of the first row
    var first = "1";
 
    //hide all rows except the first
    $("tr[id^='javatbd" + quest + "']").css("display","none");
    $("tr[id='javatbd" + quest + first + "']").css("display","table-row");
 
    //display rows if previous is answered
    $("[name^='" + quest + "']").focusin(function() {
    	$("#" + $(this).parent().parent().attr('id')).next().css("display","table-row");
    });
});
//END
 
</script>

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now