I have a checkbox with dynamic list item.I want to push the checked item at the top of the list in onload the list.
I have tried as in below snippet.I tried to change the color of the checked item.color get changed but not pushed the item to top of the list.
$(document).ready(function(){
$("input[name='geo[]']").each(function(){
if ($(this).is(":checked")){
$(this).prop("checked",true);
$(this).parent('li').css('background-color','#3875D7');
$(this).parent('li').prepend($(this));
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<ul class="checklist">
<li tabindex="0" class="even" ><input type="checkbox" value="US_East" name="geo[]" id="geo_US_East" ><label for="geo_US_East" class="leaveRoomForCheckbox">US East</label></li>
<li tabindex="0" class="odd" ><input type="checkbox" value="US_West" name="geo[]" id="geo_US_West" ><label for="geo_US_West" class="leaveRoomForCheckbox">US West</label></li>
<li tabindex="0" class="even checked" ><input type="checkbox" value="NSU" name="geo[]" id="geo_NSU" checked="checked"><label for="geo_NSU" class="leaveRoomForCheckbox">NSU</label></li>
</ul>