php – Javascript for toggle checkbox – Laravel-ThrowExceptions

Exception or error:

Hello guys I’m new to Laravel and programming, I’m trying to let the checkbox checked if the value of $status is 1, in my scenario below the value of {{$detail->status}} should be 1 so the checkbox should be checked but its not working.

Javascript

<script>
function sta() {
  var st = document.getElementById("status").value;
  var checkbox = document.getElementById("status");

  if (st == 1) {
        checkbox.checked == true;
  } else {
        checkbox.checked == false;
  }
}
</script>

HTML

<body onload= "sta()">

<form id="ed" name="ed" method="post" action="{{action('DetailTestController@update',$id)}}">
    {{csrf_field()}}
    <input type="hidden" name="_method" value="PATCH" />
        <div class="form-group">Name:
     <input type="text" name="name" class="form-control" value="{{$detail->name}}" placeholder="Enter Name" />
        </div>
        <div class="form-group">Description:
                    <input type="textarea" name="description" class="form-control" value="{{$detail->description}}" 
                    placeholder="Enter Description" />
        </div>

//from here is the checkbox code

<div class="form-group">Status Type:
    <label class="radio-inline">
        <input type="checkbox" id="status" name="status" value="{{$detail->status}}">
    </label>
</div>
How to solve:

Try to add the checked condition in the element like,

<div class="form-group">Status Type:
            <label class="radio-inline">
               <input type="checkbox" id="status" name="status" value="{{$detail->status}}" @if ($detail->status == 1) checked @endif>
           </label>
         </div>

This will chec the chekbox if $detail->status == 1 is 1
Here you don’t need a script for it

###

What about not using Javascript for this ?

<div class="form-group">Status Type:
    class="radio-inline">
        <input type="checkbox" id="status" name="status" value="{{$detail->status}}" {{$detail->status == 1 ? 'checked' : ''}}>
    </label>
</div>

Leave a Reply

Your email address will not be published. Required fields are marked *