0

when i'm trying to check an option it the other options suppose to be unchecked automatically but it's not also i try to uncheck option manually but it doesn't work

NOTE

i can't use the same name because each role have his own id i want user just select 1 option

Radio Bottoms checked at the same time

code

<div class="col-12">
                            <div class="custom-control custom-radio mb-5">
                                <input class="custom-control-input" type="radio" name="admin" id="example-radio1" value="option1"{{$user->hasRole('Admin') ? 'checked' : ''}}>
                                <label class="custom-control-label" for="example-radio1">Admin</label>
                            </div>
                            <div class="custom-control custom-radio mb-5">
                                <input class="custom-control-input" type="radio" name="editor" id="example-radio2" value="option1"{{$user->hasRole('Editor') ? 'checked' : ''}}>
                                <label class="custom-control-label" for="example-radio2">Editor</label>
                            </div>
                            <div class="custom-control custom-radio mb-5">
                                <input class="custom-control-input" type="radio" name="user" id="example-radio3" value="option1"{{$user->hasRole('User') ? 'checked' : ''}}>
                                <label class="custom-control-label" for="example-radio3">User</label>
                            </div>
                        </div>
Hamad Essa
  • 81
  • 1
  • 8

4 Answers4

1

as @kerbholz said, radio groups should have the same name. See example below:

<div class="col-12">
        <div class="custom-control custom-radio mb-5">
                <input class="custom-control-input" type="radio" name="role" id="role-admin" value="admin"{{$user->hasRole('Admin') ? 'checked' : ''}}>
                <label class="custom-control-label" for="role-admin">Admin</label>
        </div>
        <div class="custom-control custom-radio mb-5">
                <input class="custom-control-input" type="radio" name="role" id="role-editor" value="editor"{{$user->hasRole('Editor') ? 'checked' : ''}}>
                <label class="custom-control-label" for="role-editor">Editor</label>
        </div>
        <div class="custom-control custom-radio mb-5">
                <input class="custom-control-input" type="radio" name="role" id="role-user" value="user"{{$user->hasRole('User') ? 'checked' : ''}}>
                <label class="custom-control-label" for="role-user">User</label>
        </div>
        </div>
</div>

Now, process the inputs in the controller as you want/need.

hhelderneves
  • 925
  • 8
  • 24
0

Do like that

<div class="col-12">
                                <div class="custom-control custom-radio mb-5">
                                    <input class="custom-control-input" type="radio" name="check" id="example-radio1" value="option1"{{$user->hasRole('Admin') ? 'checked' : ''}}>
                                    <label class="custom-control-label" for="example-radio1">Admin</label>
                                </div>
                                <div class="custom-control custom-radio mb-5">
                                    <input class="custom-control-input" type="radio" name="check" id="example-radio2" value="option1"{{$user->hasRole('Editor') ? 'checked' : ''}}>
                                    <label class="custom-control-label" for="example-radio2">Editor</label>
                                </div>
                                <div class="custom-control custom-radio mb-5">
                                    <input class="custom-control-input" type="radio" name="check" id="example-radio3" value="option1"{{$user->hasRole('User') ? 'checked' : ''}}>
                                    <label class="custom-control-label" for="example-radio3">User</label>
                                </div>
                            </div>
Lalit Kumar
  • 256
  • 2
  • 14
0

Use the same value of attribute name for all the radio input fields instead of using different name

<div class="col-12">
        <div class="custom-control custom-radio mb-5">
            <input class="custom-control-input" type="radio" name="role_type" id="example-radio1"
                   value="Admin"{{$user->hasRole('Admin') ? 'checked' : ''}}>
            <label class="custom-control-label" for="example-radio1">Admin</label>
        </div>
        <div class="custom-control custom-radio mb-5">
            <input class="custom-control-input" type="radio" name="role_type" id="example-radio2"
                   value="Editor"{{$user->hasRole('Editor') ? 'checked' : ''}}>
            <label class="custom-control-label" for="example-radio2">Editor</label>
        </div>
        <div class="custom-control custom-radio mb-5">
            <input class="custom-control-input" type="radio" name="role_type" id="example-radio3"
                   value="User"{{$user->hasRole('User') ? 'checked' : ''}}>
            <label class="custom-control-label" for="example-radio3">User</label>
        </div>
    </div>
Rashed Rahat
  • 2,357
  • 2
  • 18
  • 38
  • i cant use the same name because each role have his own id i want user just select 1 option – Hamad Essa Mar 02 '20 at 11:00
  • 1
    As far as i know that you have to use the same name. The reason is You can't let user to select any one option out of three until you give the same name. If you assign different value of name then more than one radio button can be selected. Hope you understand the fact. To achieve your goal assign the value attribute with Admin, Editor & User respectively. Have a look at my solution i have posted. – Rashed Rahat Mar 02 '20 at 11:10
0

To create a radio box it is necessary to use the same name for all radio input fields. If you don't want to give the same name to your radio boxes then you have another option create custom radio boxes with jquery

if($('input[name="admin"]').is(':checked')) {
    $(this).prop('checked',true);
    $('input[name="editor"]').prop('checked',false);
    $('input[name="user"]').prop('checked',false);
} else if($('input[name="editor"]').is(':checked')) {
    $(this).prop('checked',true);
    $('input[name="admin"]').prop('checked',false);
    $('input[name="user"]').prop('checked',false);
} else if($('input[name="user"]').is(':checked')) {
    $(this).prop('checked',true);
    $('input[name="admin"]').prop('checked',false);
    $('input[name="editor"]').prop('checked',false);
}

Hope it works...

UPDATE

Use following jQuery code

$(".custom-control-input").click(function(){
    var group = "input[name='"+$(this).attr("name")+"']";
    $(group).attr("checked",false);
    $(this).attr("checked",true);
});

Example URL

Lokesh Suman
  • 493
  • 5
  • 14