This script will only show the input elements in row-4 if a specific value is selected in row 3. In this case, I have set the value to "4". Modify the "
Code:
<script type="text/javascript" charset="utf-8">
$(document).on('ready pjax:scriptcomplete',function(){
var thisQuestion = $('#question{QID}');
// Insert selects into row 2
if($('tr.subquestion-list:eq(1) .answer-item .inserted-select', thisQuestion).length == 0) {
$('tr.subquestion-list:eq(1) .answer-item', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
<option value="">Please choose...</option>\
<option value="1">Yes</option>\
<option value="2">No</option>\
<option value="3">Do not know</option>\
</select>');
}
// Insert selects into row 3
if($('tr.subquestion-list:eq(2) .answer-item .inserted-select', thisQuestion).length == 0) {
$('tr.subquestion-list:eq(2) .answer-item', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
<option value="">Please choose...</option>\
<option value="1">Red</option>\
<option value="2">Blue</option>\
<option value="3">Pink</option>\
<option value="4">Show row-4 input</option>\
</select>');
}
// Listeners on select elements
$('.inserted-select', thisQuestion).on('change', function(i) {
if($(this).val() != '') {
$(this).closest('.answer-item').find('input:text').val($.trim($('option:selected', this).text())).trigger('change');
}
else {
$(this).closest('.answer-item').find('input:text').val('').trigger('change');
}
});
// Returning to page
$('.with-select input:text', thisQuestion).each(function(i) {
var thisCell = $(this).closest('.answer-item');
var inputText = $.trim($(this).val());
var selectval = $('select.inserted-select option', thisCell).filter(function () { return $(this).html() == inputText; }).val();
$('select.inserted-select', thisCell).val(selectval);
});
// Numeric only in row 4
$('tr.subquestion-list:eq(3) input:text', thisQuestion).on('keyup change', function(e) {
var thisValue = $.trim($(this).val());
if($.isNumeric(thisValue) === false) {
// Strip out non-numerics characters
newValue = thisValue.replace(/\D/g,'');
$(this).val(newValue);
checkconditions($(this).val(), $(this).attr('name'), 'text');
}
});
// Hide row-4 inputs unless a specific value selected in row 3
var showValue = '4';
// Listener on row-3 selects
$('tr.subquestion-list:eq(2) select', thisQuestion).on('change', function(e) {
var thisValue = $(this).val();
var thisCode = $(this).closest('.answer-item').find('input:text').attr('id').split('_')[1];
var otherInput = $('tr.subquestion-list:eq(3) .answer_cell_'+thisCode+' input:text', thisQuestion);
if(thisValue == showValue) {
$(otherInput).show();
}
else {
$(otherInput).hide().val('');
checkconditions($(otherInput).val(), $(otherInput).attr('name'), 'text');
}
});
// Initial states of row-4 inputs
$('tr.subquestion-list:eq(3) input:text', thisQuestion).hide();
$('tr.subquestion-list:eq(2) .inserted-select', thisQuestion).each(function(i) {
var thisValue = $(this).val();
var thisCode = $(this).closest('.answer-item').find('input:text').attr('id').split('_')[1];
var otherInput = $('tr.subquestion-list:eq(3) .answer_cell_'+thisCode+' input:text', thisQuestion);
if(thisValue == showValue) {
$('tr.subquestion-list:eq(3) .answer_cell_'+thisCode+' input:text', thisQuestion).show();
}
});
// Clean-up styles
$('select.inserted-select', thisQuestion).css({
'max-width': '100%'
});
$('.with-select input:text', thisQuestion).css({
'position': 'absolute',
'left': '-9999em'
});
});
</script>