Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

dynamic rows to an Array (Multi Flexible) in LimeSurvey 2.5

  • brunogalvao
  • brunogalvao's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 5 months ago #143902 by brunogalvao
Hello,

I have problems adding rows.
Use the following code, i see the "+" and "-" but nothing happens.
Can anyone help?

$(document).ready(function() {

// A function to add or remove rows of an Array (Multi Flexible)(Text) question
function varLengthArray(qID) {

if ($('#question'+qID+'').length > 0) {

// The HTML content of the Add/Remove elements - modify as you wish
var addContent = '[+]';
var removeContent = '[-]';

// Insert the controls
$('#question'+qID+' table.subquestion-list' ).after('<div id="addButton'+qID+'">'+addContent+'</div><div id="removeButton'+qID+'" style="display:none;">'+removeContent+'</div>');

// Style the controls - you can modify here if you wish
$( 'div#addButton'+qID ).css({
'margin':'10px 0 0 10px',
'padding':'1px',
'text-align':'center',
'font-weight':'bold',
'width':'auto',
'cursor':'pointer',
'float':'left'
});
$( 'div#removeButton'+qID ).css({
'margin':'10px 0 0 10px',
'padding':'1px',
'text-align':'center',
'font-weight':'bold',
'width':'auto',
'cursor':'pointer',
'float':'left'
});

// Call the functions below when clicked
$( 'div#addButton'+qID ).click(function (event) {
addRow(qID);
});
$( 'div#removeButton'+qID ).click(function (event) {
removeRow(qID);
});

// Function to add a row, also shows the Remove element and hides the
//Add element if all rows are shown
function addRow(qID) {
$('#question'+qID+' tr.subquestion-list[name="hidden"]:first' ).attr('name', 'visible').show();
$( 'div#removeButton'+qID ).show();
if ($('#question'+qID+' tr.subquestion-list:eq(' + rowCount + ')' ).attr('name') == 'visible' ) {
$( 'div#addButton'+qID ).hide();
}
}
// Function to remove a row, also clears the contents of the removed row,
// shows the Add element if the last row is hidden and hides the Remove
// element if only the first row is shown
function removeRow(qID) {
$('#question'+qID+' tr.subquestion-list[name="visible"]:last input[type="text"]' ).val('');
$('#question'+qID+' tr.subquestion-list[name="visible"]:last' ).attr('name', 'hidden').hide();
$( 'div#addButton'+qID ).show();
if ($('#question'+qID+' tr.subquestion-list:eq(1)').attr('name') == 'hidden' ) {
$( 'div#removeButton'+qID ).hide();
}
}

// Some initialization stuff
var arrayRow = $('#question'+qID+' tr.subquestion-list');
var rowCount = '';

// Initially hide all except first row or any rows with populated inputs
$( arrayRow ).each(function(i) {
if ( i > 0 ) {
// We also need to give the hidden rows a name cause older IE doesn't
// recognize jQuery :visible selector consistently
$( this ).attr('name', 'hidden').hide();
$('input[type=text]', this).each(function(i) {
if ($(this).attr('value') !== '') {
$(this).closest('tr.subquestion-list').attr('name', 'visible').show();
$( 'div#removeButton'+qID ).show();
}
});
rowCount = i;
}
});
}
}

// Call the function with a question ID
varLengthArray(17);
});
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 5 months ago #143906 by holch
This code was most probably written for a previous version of LS and might not work with the newer version that you are using. A lot of workarounds don't work anymore since 2.5 and will need some update.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose