0

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>
2
  • you just need to add one more property for input checkbox Commented Feb 24, 2020 at 8:52
  • <input type="checkbox" id="status" name="status" value="{{$detail->status}}" {{ $detail->status == 1 ? "checked" : "" }}> Commented Feb 24, 2020 at 8:53

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.