massex12
massex12

Reputation: 33

Turn CheckBoxes into CheckButtons

I am trying to turn These checkboxes:

enter image description here

Into These Checkboxes(I will refer to these as CheckButtons):

enter image description here

Directly below is the code of the current Check Boxes:

        @foreach (var department in Model.Select(u => new { u.DepartmentId, u.DepartmentName }).Distinct().ToDictionary(u => u.DepartmentId, u => u.DepartmentName).OrderBy(u => u.Value))
    {
        i++;
        <text> &nbsp; &nbsp;</text>
@department.Value <input name="department_chkbox" type="checkbox" value="@department.Key" />
        if (i > 5)
        {
            <text><br></text>
            i = 0;
        }
    }

The HTML of the desired ones is below but it does not tell me much:

<td id="checkboxcontainer">
     <input type="checkbox" name="statusId" value="1" id="ckActive" checked="checked" /><label for="ckActive">Active</label>
     <input type="checkbox" name="statusId" value="2" id="ckLeave" /><label for="ckLeave">Leave</label>
     <input type="checkbox" name="statusId" value="3" id="ckSusp" /><label for="ckSusp">Suspended</label>
     <input type="checkbox" name="statusId" value="4" id="ckTerm" /><label for="ckTerm">Terminated</label>
</td>

Does anyone know what is being called to make the checkboxes turn into "checkbuttons" I wrote the check box code, but I do not have access to the check button code. Im assuming that this is something that is done in eitehr Javascript or Jquery. Also there is no class for the

Upvotes: 1

Views: 41

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 149020

Going off of the desired HTML, it's relatively simple solution using only CSS. Of course, you'll want to tweak it to get it looking exactly the way you want.

#checkboxcontainer {
  display: flex;
}

input[type=checkbox] {
  display: none;
}

input[type=checkbox] + label {
  display: block;
  min-width: 100px;
  border: solid #999;
  border-width: 1px 1px 1px 0;
  background: #eee;
  margin: 0;
  padding: 2px 0;
  text-align: center;
}

input[type=checkbox]:checked + label {
  background: #ccc;
}

input[type=checkbox] + label:first-of-type {
  border-radius: 3px 0 0 3px;
  border-left-width: 1px;
}
input[type=checkbox] + label:last-of-type {
  border-radius: 0 3px 3px 0;
}
<div id="checkboxcontainer">
  <input type="checkbox" name="statusId" value="1" id="ckActive" checked="checked" /><label for="ckActive">Active</label>
  <input type="checkbox" name="statusId" value="2" id="ckLeave" /><label for="ckLeave">Leave</label>
  <input type="checkbox" name="statusId" value="3" id="ckSusp" /><label for="ckSusp">Suspended</label>
  <input type="checkbox" name="statusId" value="4" id="ckTerm" /><label for="ckTerm">Terminated</label>
</div>

And to generate this HTML using Razor you'd have something like this:

<div id="checkboxcontainer">
  @foreach (var department in Model.Select(u => new { u.DepartmentId, u.DepartmentName }).Distinct().ToDictionary(u => u.DepartmentId, u => u.DepartmentName).OrderBy(u => u.Value))
  {
    i++;
    <input name="department_chkbox" type="checkbox" value="@department.Key" id="department_chkbox@(i)" /><label for="department_chkbox@(i)">@department.Value</label>
  }
</div>

Upvotes: 4

Related Questions