- Posts: 18
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Javascript for setting default values in radio button array
- Baarceri_122875
- Topic Author
- Offline
- Banned
Less
More
6 years 3 months ago - 6 years 3 months ago #162614
by Baarceri_122875
Javascript for setting default values in radio button array was created by Baarceri_122875
Hi there,
I am trying to adjust some Javascript to set the default radio button in an array question. I'm not familiar with Javascript, so don't know what I'm doing wrong. Here is my code:
I am trying to adjust some Javascript to set the default radio button in an array question. I'm not familiar with Javascript, so don't know what I'm doing wrong. Here is my code:
Code:
What kinds of support and resources would {orgname} prefer to receive through the project? <script type="text/javascript" charset="utf-8"> $(document).ready(function() { $('#question{QID} tr[id^="javatbd"]').each(function(i) { if($('#question{QID} input.radio:checked').length == 0) { document.getElementById('answer{SGQ}-A3').checked = 'checked'; }) }) }); </script>
Last edit: 6 years 3 months ago by Baarceri_122875.
The topic has been locked.
- Baarceri_122875
- Topic Author
- Offline
- Banned
Less
More
- Posts: 18
- Thank you received: 0
6 years 3 months ago - 6 years 3 months ago #162623
by Baarceri_122875
Replied by Baarceri_122875 on topic Javascript for setting default values in radio button array
I've managed to get it to work with the following code, but would like to get rid of the repetition. Anyone know the way to make it work for all sub-questions with one line of code?
Code:
What kinds of support and resources would {orgname} prefer to receive through the project? <script type="text/javascript" charset="utf-8"> $(document).ready(function() { if($('#question{QID} input.radio:checked').length == 0) { document.getElementById('answer{SGQ}SQ001-A3').checked = 'checked'; document.getElementById('answer{SGQ}SQ002-A3').checked = 'checked'; document.getElementById('answer{SGQ}SQ003-A3').checked = 'checked'; document.getElementById('answer{SGQ}SQ004-A3').checked = 'checked'; document.getElementById('answer{SGQ}SQ005-A3').checked = 'checked'; document.getElementById('answer{SGQ}SQ006-A3').checked = 'checked'; document.getElementById('answer{SGQ}SQ007-A3').checked = 'checked'; document.getElementById('answer{SGQ}SQ008-A3').checked = 'checked'; } }); </script>
Last edit: 6 years 3 months ago by Baarceri_122875. Reason: Clearer formatting
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10099
- Thank you received: 3589
6 years 3 months ago #162625
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Javascript for setting default values in radio button array
Try this:
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { $('#question{QID} tr[id^="javatbd"]').each(function(i) { if($('input.radio:checked', this).length == 0) { var radio = $('input.radio[value="A3"]', this); $(radio).prop('checked', true); checkconditions($(radio).val(), $(radio).attr('name'), 'radio'); } }); }); </script>
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: Baarceri_122875
The topic has been locked.
- Baarceri_122875
- Topic Author
- Offline
- Banned
Less
More
- Posts: 18
- Thank you received: 0
6 years 3 months ago #162627
by Baarceri_122875
Replied by Baarceri_122875 on topic Javascript for setting default values in radio button array
Thanks Tony, that's perfect!
Will
Will
The topic has been locked.
- achecchini
- Offline
- Senior Member
Less
More
- Posts: 61
- Thank you received: 0
6 years 1 month ago #164535
by achecchini
Replied by achecchini on topic Javascript for setting default values in radio button array
Hi! Have a nice day!
I was in trouble about this piece of javascript code that's perfectly functioning on 2.05 version but NOT under 3.04
The script I try is:
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
// Identify this question ID
var qID = {QID};
// Loop through the rows and check the first radio if none are already checked
$('#question'+qID+' table.question tbody tr').each(function(i) {
if($('input.radio:checked', this).length == 0) {
$('input.radio:eq(0)', this).attr('checked', true);
}
});
});
</script>
Of course I try with your proposal too, but the effect in none in both cases: no checked radio button!
What is wrong or mistake ?
Thanks in advance for any suggestion ...
I was in trouble about this piece of javascript code that's perfectly functioning on 2.05 version but NOT under 3.04
The script I try is:
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
// Identify this question ID
var qID = {QID};
// Loop through the rows and check the first radio if none are already checked
$('#question'+qID+' table.question tbody tr').each(function(i) {
if($('input.radio:checked', this).length == 0) {
$('input.radio:eq(0)', this).attr('checked', true);
}
});
});
</script>
Of course I try with your proposal too, but the effect in none in both cases: no checked radio button!
What is wrong or mistake ?
Thanks in advance for any suggestion ...
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10099
- Thank you received: 3589
6 years 1 month ago #164541
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Javascript for setting default values in radio button array
This script will set the first column as defaults in 3.4.x:
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function(){ // Identify this question ID var qID = {QID}; // Loop through the rows and check the first radio if none are already checked $('#question'+qID+' .table-array-radio tr.answers-list').each(function(i) { if($('input[type="radio"]:checked', this).length == 0) { $('input[type="radio"]:eq(0)', this).trigger('click'); } }); }); </script>
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
- achecchini
- Offline
- Senior Member
Less
More
- Posts: 61
- Thank you received: 0
6 years 1 month ago #164544
by achecchini
Replied by achecchini on topic Javascript for setting default values in radio button array
Tanks so much Tony! Now is perfectly working
A.
A.
The topic has been locked.