Reputation: 697
I am using bootstrap, and am trying to convert checkboxes into a button that shows when it is pressed or not(checked or not). If I keep the code showing the checkboxes, it works perfect. If I keep the code as labelled buttons, the "checked" buttons do not load properly(they do save).
Solved : It turns out that I just needed to echo out "btn-group active" for the already active checkboxes.
Checkbox Code w/checkboxes(That works)
<div class="btn-group" id="salesman" data-toggle="buttons"></div>
<h5>Salesman :</h5>
<?php
$salesman = json_decode($invoice['Invoice']['salesman'], true);
$salesman_names = array(1 => "User1",2 => "User2",3 => "User3",4 => "User4",5 => "User5");
foreach ($salesman_names AS $i => $name) {
if ($salesman[$name] == "checked") {
echo '<label class="btn btn-default"><input type="checkbox" name="data-invoice-salesman[]" value="'.$i.'" checked/> '.$name.'</label>';
} else {
echo '<label class="btn btn-default"><input type="checkbox" name="data-invoice-salesman[]" value="'.$i.'" /> '.$name.'</label>';
}
}
?>
Checkbox Code w/out Checkboxes(that needs fixed?)
<div class="btn-group" id="salesman" data-toggle="buttons">
<h5>Salesman :</h5>
<?php
$salesman = json_decode($invoice['Invoice']['salesman'], true);
$salesman_names = array(1 => "User1",2 => "User2",3 => "User3",4 => "User4",5 => "User5");
foreach ($salesman_names AS $i => $name) {
if ($salesman[$name] == "checked") {
echo '<label class="btn btn-default"><input type="checkbox" name="data-invoice-salesman[]" value="'.$i.'" checked/> '.$name.'</label>';
} else {
echo '<label class="btn btn-default"><input type="checkbox" name="data-invoice-salesman[]" value="'.$i.'" /> '.$name.'</label>';
}
}
?>
Upvotes: 1
Views: 1126
Reputation: 67525
I think you search a toggle buttons, check example bellow.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default active">
<input type="checkbox" checked autocomplete="off"> Checkbox 1 (pre-checked)
</label>
<label class="btn btn-default">
<input type="checkbox" autocomplete="off"> Checkbox 2
</label>
<label class="btn btn-default">
<input type="checkbox" autocomplete="off"> Checkbox 3
</label>
</div>
Upvotes: 1
Reputation: 24965
From what I gather you want to link the clicking of a label to the toggling of a checkbox (which could be hidden).
<label for="me">
Blah1
<input type="checkbox" id="me">
</label>
Upvotes: 0