Disabling a field when user selects a radio button
Disabling a field when user selects a radio button
caneco2013
Posts: 6Questions: 0Answers: 0
Hello. Is there a way to disable a field or fields when a user clicks a checkbox or radio button or even choosing an option in a dropdown box/select field? It seems I can't find any similar discussion here in the forum.
Here is that code that I am having a problem right now:
[code]
Field::inst( 'basement_student_room' )
->validator( 'Validate::required' ),
Field::inst( 'basement_condition' )
->validator( 'Validate::required' ),
[/code]
'basement_student_room' and 'basement_condition' are both radio buttons. 'basement_student_room' is a "Yes" and "No" radio buttons while 'basement_condition' is a set of typical basement conditions like musty etc. Now, what I want to do is disable 'basement_condition' whenever 'basement_student_room' returns a "No" value.
Now what I am thinking right now is that I need to create a custom validation that will handle such scenario arriving to this kind of idea:
[code]
Field::inst( 'basement_student_room' )
->validator( function($val){
if($val == 0){
'basement_condition' == disabled;
}
else{
'basement_condition' == enabled;
}
}),
Field::inst( 'basement_condition' )
->validator( ),
[/code]
Am I on the right track here?
Here is that code that I am having a problem right now:
[code]
Field::inst( 'basement_student_room' )
->validator( 'Validate::required' ),
Field::inst( 'basement_condition' )
->validator( 'Validate::required' ),
[/code]
'basement_student_room' and 'basement_condition' are both radio buttons. 'basement_student_room' is a "Yes" and "No" radio buttons while 'basement_condition' is a set of typical basement conditions like musty etc. Now, what I want to do is disable 'basement_condition' whenever 'basement_student_room' returns a "No" value.
Now what I am thinking right now is that I need to create a custom validation that will handle such scenario arriving to this kind of idea:
[code]
Field::inst( 'basement_student_room' )
->validator( function($val){
if($val == 0){
'basement_condition' == disabled;
}
else{
'basement_condition' == enabled;
}
}),
Field::inst( 'basement_condition' )
->validator( ),
[/code]
Am I on the right track here?
This discussion has been closed.
Replies
You can use the `node()` method to get the radio fields so you can attach event handlers and the `enable()` and `disable()` methods to enable and disable the sub-field as needed. For example:
[code]
$( 'input', editor.node( 'basement_condition' ) ).on( 'change', function () {
if ( editor.get( 'basement_condition' ) == 0 ) {
editor.disable( 'base_student_room' );
}
else {
editor.enable( 'base_student_room' );
}
} );
[/code]
Allan