nionta's avatar

update attribute using radio button

I have two radio button named company_status in my #edit-modal which values are active & inactive; when I will click on a edit button , based on the existing status of the company, the radio button will be selected automatically; how can I do that?

here is my view file code:

                <tr>
                                    <td>
                                        <input type="checkbox" name="checkbox_for_delete_multi_id[]"    value="{{$company->id}}">
                                    </td>
                                    <td class="com_name">{{$company->company_name}}</td>
                                    <td class="com_status">{{$company->company_status}}</td>
                                    <td name="created_at">{{$company->created_at}}</td>
                                    <td name="updated_at">{{$company->updated_at}}</td>
                                    <td>
                                        <a href="#" class="edit-model" data-toggle="modal" data-target="#EditModel" data-id="{{$company->id}}" data-company_name="{{$company->company_name}}" data-status="{{$company->company_status}}">
                                            <span style="color: green"><i class="glyphicon glyphicon-edit"></i></span>
                                        </a>
                                    </td>
                                </tr>




        <div class="modal fade" id="EditModel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Edit Company Info</h4>
            </div>

            <form id="company_edit_form" method="post">
                {{ csrf_field() }}
                <div class="modal-body">
                    <div class="form-group">
                        <h5>Company Name</h5>
                        <input class="form-control" id="edited_company_name" name="edited_company_name">
                    </div>
                    <div class="form-group">
                        <h5>Company Name</h5>
                        <input type="radio" class="edited_company_status" name="edited_company_status"  value="active">

                        <input type="radio" value="inactive" class="edited_company_status" name="edited_company_status">

            </div>

and here is my script:

          $('#EditModel').on('show.bs.modal', function (event) {
            var button = $(event.relatedTarget) // Button that triggered the modal
            var company_name = button.data('company_name') // Extract info from data-* attributes
            // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
            // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
            var modal = $(this)
            modal.find('.modal-body #edited_company_name').val(company_name);
            //modal.find('.modal-body #edited_company_staus').val(company_name);
        })
0 likes
1 reply
Cronix's avatar

To check a checkbox/radio button, you'd use

$(element).prop("checked", true);

to uncheck, you'd use false

So this will probably work for your case.

// Im assuming company_status will be "active" or "inactive"
var company_status = button.data('company_status');
var active = $('.modal-body [value="active"]'); // active radio button
var inactive = $('.modal-body [value="inactive"]'); // inactive radio button

// if company status is "active", check the radio button that has value of active, uncheck otherwise
if (company_status == 'active') {
    active.prop('checked', true);
    inactive.prop('checked', false);
} else {
    active.prop('checked', false);
    inactive.prop('checked', true);
}
1 like

Please or to participate in this conversation.