jquery/javascript perform function on checkbox click or adjacent text click -
i have unordered list of text items checkboxes beside each of them. want perform same function both when checkbox checked/unchecked , when text clicked.
here's have right now:
the html
<li><input type="checkbox" class="cbox" name="adwords_list[]" value="shave club" ><p class="term"> shave club</p></li>
the js
$(function() { $('ol.phrases li .term').click(function() { $cboxstatus = $(this).parent().find('.cbox').prop('checked'); if ($cboxstatus) { $(this).parent().find('.cbox').prop('checked', false); } else { $(this).parent().find('.cbox').prop('checked', true); } }); }); $(':checkbox, .term').on('change', function() { console.log($(this).prop('checked')); });
the on.change
function isn't calling when term clicked. can accurate representation of checked state no matter element clicked/changed?
if change p.term
elements label
, appropriate for
attributes matching associated checkboxes, clicking text same checking box. need watch checkbox events.
<input type="checkbox" class="cbox" name="adwords_list[]" value="shave club" id="cb1" /> <label class="term" for="cb1"> shave club</label>
you can add
.term { display: block; }
etc. css if need match styling of p
tag.
Comments
Post a Comment