1

Dynamically added checkbox inside jQuerymobile list view is not rendering properly.

Find below the unorder list. I have added a sample listitem in the HTML itself.

<ul data-role="listview" id="ul_address_list" >
  <li data-icon="false"><a href="#" class="add-container">
    <label class="add-container" data-corners="false">
      <input type="checkbox" value="true" />
      <label class="lbl_add-container">
        <h3>Header</h3>
        <p>Content</p>
      </label>
    </label>
  </li>
</ul>

Below is the output for the above code. Which is rendering correct.

The correct display

Now I am trying to add the list item dynamically using append function using jQuery.

$.each(obj_address_list, function (ctr, obj) {
    $('#ul_address_list').append('<li data-icon="false">' +
        '<a href="#" class="add-container">' +
        '<label class="add-container" data-corners="false">' +
        '<input type="checkbox" value="true" />' +
        '<label class="lbl_add-container">' +
        '<h3>Header</h3>' +
        '<p>content</p></div>' +
        '</label>' +
        '</label>' +
        '</a>' +
        '</li>');
});
$('#ul_address_list').listview('refresh');

below is the output for the above code. Not displayed correctly.

Not displayed correctly

Why the listitem added dynamically is not rendering properly?

3 Answers 3

2

Just add

$("[type=checkbox]").checkboxradio();

before

$('#ul_address_list').listview('refresh');

Or, call .trigger("create");

$('#ul_address_list').listview('refresh').trigger("create");

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

both of your suggestions are working fine. Thanks a lot. I put the code like this $("input[name=chk_address]").checkboxradio(); and this also works fine.
2

You need to call the listview refresh inside your append function and add .trigger("create");.

$('#ul_address_list').append('<li data-icon="false">' +
  '<a href="#" class="add-container">' +
    '<label class="add-container" data-corners="false">' +
      '<input type="checkbox" value="true" />' +
      '<label class="lbl_add-container">' +
        '<h3>Header</h3>' +
        '<p>content</p></div>' +
      '</label>' +
    '</label>' +
  '</a>' +
'</li>').listview("refresh");

Check this fiddle: http://jsfiddle.net/eRsMV/5

2 Comments

If you see my example, it shows that adding the same thing you added in your answer, it's appending the new list element with the same css properties applied.
Simply that i forgot to add it on my answer. It's .trigger("create"); that was missing. jsfiddle.net/eRsMV/5
0

you have extra

});

just use

$('#ul_address_list').append('<li data-icon="false">' +
  '<a href="#" class="add-container">' +
    '<label class="add-container" data-corners="false">' +
      '<input type="checkbox" value="true" />' +
      '<label class="lbl_add-container">' +
        '<h3>Header</h3>' +
        '<p>content</p></div>' +
      '</label>' +
    '</label>' +
  '</a>' +
'</li>');

$('#ul_address_list').listview('refresh');

my advice is to use some editor like dreamweaver just to save your time.

http://jsfiddle.net/prollygeek/xqwqM/1/

4 Comments

@prollyGeed - Sorry that is a copy paste error. I updated the question.
$('#ul_address_list').listview('refresh').trigger("create"); this is the correct answer , given by Omar
obj_address_list is a object list which will keep the for each loop to run for n times. I haven't copied the entire code. After adding $("[type=checkbox]").checkboxradio(); it works fine. Tks
ok and your code work properely by the way , check jsfiddle.net/prollygeek/xqwqM/1 just as you said it needed to trigger checkbox once again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.