Welcome, Guest
Username: Password: Remember me

TOPIC: new row appears only when the previous row has been filled

new row appears only when the previous row has been filled 3 months 1 week ago #111587

  • qdrop
  • qdrop's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 5
  • Karma: 0
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: 3 months 1 week ago by qdrop. Reason: removing typos
The administrator has disabled public write access.

new row appears only when the previous row has been filled 3 months 1 week ago #111627

  • qdrop
  • qdrop's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 5
  • Karma: 0
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...
The administrator has disabled public write access.

new row appears only when the previous row has been filled 3 months 1 week ago #111639

  • qdrop
  • qdrop's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 5
  • Karma: 0
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>
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.190 seconds
Donation Image