Hi,
sorry, I've been on the road the last days...
Thanks tpartner for the new Javascript - it works like a charm!
I would just have one more request: I am using this script also for a bigger table, where columns 1, 3 and 5 are using this script, while 2, 4 and 6 are numerical values. I used your new script and one older script to assign the dropdowns to the columns 1, 3 and 5. This is the result:
Code:
<script type="text/javascript" data-author="Tony Partner">
$(document).on('ready pjax:scriptcomplete',function(){
var thisQuestion = $('#question{QID}');
if($('.inserted-select', thisQuestion).length == 0) {
// Column-specific classes
$('tr.subquestion-list', thisQuestion).each(function(i) {
$('th, td', this).each(function(i) {
$(this).addClass('column-'+i);
});
});
// Insert selects into column 1
if($('.answer-item.column-1 .inserted-select', thisQuestion).length == 0) {
$('.answer-item.column-1', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
<option value="" selected>Bitte auswählen...</option>\
<option value="1">Zunahme um</option>\
<option value="0">Gleichbleibend</option>\
<option value="2">Abnahme um</option>\
</select>');
}
// Insert selects into column 3
if($('.answer-item.column-3 .inserted-select', thisQuestion).length == 0) {
$('.answer-item.column-3', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
<option value="" selected>Bitte auswählen...</option>\
<option value="1">Zunahme um</option>\
<option value="0">Gleichbleibend</option>\
<option value="2">Abnahme um</option>\
</select>');
}
// Insert selects into column 5
if($('.answer-item.column-5 .inserted-select', thisQuestion).length == 0) {
$('.answer-item.column-5', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
<option value="" selected>Bitte auswählen...</option>\
<option value="1">Zunahme um</option>\
<option value="0">Gleichbleibend</option>\
<option value="2">Abnahme um</option>\
</select>');
}
// Listeners
$('.inserted-select', thisQuestion).on('change', function(i) {
if($(this).val() != '') {
$(this).closest('.answer-item').find('input:text').val($('option:selected', this).val()).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());
$('select.inserted-select', thisCell).val(inputText);
});
// Listener on column 2 inputs
$('.answer-item.answer_cell_change input:text', thisQuestion).on('keyup change', function(e) {
var thisValue = $.trim($(this).val());
// Numerics only
if($.isNumeric(thisValue) === false) {
// Strip out non-numerics characters
newValue = thisValue.replace(/\D/g,'');
$(this).val(newValue).trigger('change');
}
// Max/min values
var maxAllowed = 100;
var minAllowed = 0;
if (thisValue > maxAllowed) {
alert('The maximum allowed is '+maxAllowed+'.');
$(this).val(maxAllowed);
}
else if (thisValue < minAllowed) {
alert('The minimum allowed is '+minAllowed+'.');
$(this).val(minAllowed);
}
});
// Clean-up styles
$('select.inserted-select', thisQuestion).css({
'max-width': '100%'
});
$('.with-select input:text', thisQuestion).css({
'position': 'absolute',
'left': '-9999em'
});
}
});
</script>
Is this code still correct?
Thanks,
Ralf