javascript - How to stop Duplicate value in this code -
how stop duplicate value in code
html
<ul class="care-ul"> <li><a><input type="checkbox" value="disease" /> disease</a></li> <li><a><input type="checkbox" value="failure"/> failure</a></li> <li><a><input type="checkbox" value="wounds"/> wounds</a></li> <li><a><input type="checkbox" value="afib"/> afib</a></li> </ul>
javascript
var carevalue = []; function questioncount() { $.each($('input[type="checkbox"]:checked'), function () { var value = $(this).val(); carevalue.push(value); }); var json = json.stringify(carevalue); var obj = json.parse(json); $('.new-lists li').html(obj); } $(document).ready(function () { $('#apply').click(function () { questioncount(); }); });
you need intialize carevalue
array in questioncount
method everytime rather intializing outside.
function questioncount() { carevalue = []; //now lose initial values new unique values coming $.each($('input[type="checkbox"]:checked'), function () { var value = $(this).val(); carevalue.push(value); }); var json = json.stringify(carevalue); var obj = json.parse(json); $('.new-lists li').html(obj); }
Comments
Post a Comment