lukassz lukassz - 8 months ago 25
Javascript Question

jQuery append, when I delete record view has changed baldy

I list some values using


<div data-role="main" class="ui-content">

<h2 id="list-filter">Lista produktów z lodówki</h2>
<div data-demo-html="true">
<ul data-role="listview" id="produktList" data-filter="true" data-filter-placeholder="Search fruits..." data-inset="true">


<div id="deleteButton"><input type="button" id="merge_button" value="Usuń"></div>
</div><!--/demo-html -->


And JS

function iterateOverLocalStorage() {
for(var i=0; i < localStorage.length; i++)
$('ul#produktList').append("<li class='ui-li-static ui-body-inherit ui-first-child ui-last-child'>"+localStorage.getItem(localStorage.key(i))+"" +
"<span><input type='checkbox' id='check' name='produkty[]' value='"+i+"'></span></li>");


It looks ok

enter image description here

When I click
button and this action was deleted selected record

var searchIDs = $("input:checkbox:checked").map(function(){

return this.value;

}).toArray(); // <----

if(searchIDs.length == 0)
alert('Nic nie zaznaczyles');
}else {

for (var k in searchIDs) {
console.log("Element: " + k);




The view changes, why?

enter image description here


The second view is actually the right one, with the radio button "bullet" next to the label.

The first view is different because it was built during page load, which happens before any other post-processing of the HTML occurs. Initially the div tags are not horizontally stretched to the page width, and so when your code runs the radio buttons wrap around.

It seems like you are using jQuery Mobile, which performs such post-processing ("enhancement" phase) and stretches the div tags horizontally.

You should turn the order around, and make sure you only inject the radio buttons once the page has been loaded and treated by jQuery Mobile.

This you can do as follows. Replace this line (the one which occurs outside any function):


with this:


Note that there are several events that get triggered when jQuery Mobile pages load or transition. Check out this image:

enter image description here