javascript - How to uncheck checkbox element after deleting it from append element on onclick event of image? -
here jsfiddlei want uncheck checkbox when element delete div.here appending checkbox content div delete link.
function show() { var checkboxes = document.getelementsbyname("trainig_by_industry1"); document.getelementbyid("selectbox").innerhtml = ""; (var = 0; < checkboxes.length; i++) { if (checkboxes[i].checked) { var da = checkboxes[i].value; document.getelementbyid("selectbox").innerhtml += '<span class="tagremove" >' + da + ' <img src="resources/images/cancel-128.png" id="unc" style="height:20px; width:20px;" onclick="$(this).parent().remove(); check1();"/></span>'; } } } function check1() { $('#subc1 input:checkbox').removeattr('checked'); //$(checkboxes[i]).attr('checked',false); }
one solution add data attribute
each checkbox
1,2,3.. this:
<label for="one"><input type="checkbox" data-index="1" class="check" name="trainig_by_industry1" onchange="show()" value="first checkbox" />first checkbox</label> ^^^^^^^^^^^^^^
then in js:
add data-index corrosponding create.
document.getelementbyid("selectbox").innerhtml+= '<span class="tagremove" data-index='+checkboxes[i].getattribute('data-index')+' >'+ da+ ' <img src="" alt="x" id="unc" style="height:20px; width:20px;"/></span>'; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
then attach click event new span class tagremove
instead of writing onclick
:
$(document).on('click', '.tagremove' , function(){ $('#subc1').find('[data-index="'+$(this).data("index")+'"]').prop('checked',false); $(this).remove(); })
see fiddle
Comments
Post a Comment