Javascript for setting default values in radio button array

More
1 month 5 days ago - 1 month 5 days ago #162614 by DrWillParry
DrWillParry created the topic: Javascript for setting default values in radio button array
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:
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: 1 month 5 days ago by DrWillParry.

Please Log in to join the conversation.

More
1 month 5 days ago - 1 month 5 days ago #162623 by DrWillParry
DrWillParry replied the 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?
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: 1 month 5 days ago by DrWillParry. Reason: Clearer formatting

Please Log in to join the conversation.

More
1 month 5 days ago #162625 by tpartner
tpartner replied the topic: Javascript for setting default values in radio button array
Try this:

<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: DrWillParry

Please Log in to join the conversation.

More
1 month 5 days ago #162627 by DrWillParry
DrWillParry replied the topic: Javascript for setting default values in radio button array
Thanks Tony, that's perfect!

Will

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!