I have a listview. I need to add and remove from the list. On adding to the list, the jquery mobile styling does not get added to the new content.
<ul data-role="listview" id="contributionList">
<li id="l1"><a>5.00</a><a data-icon="delete" data-role="button" id="1"></a></li>
<li><a>10.00</a><a data-icon="delete" data-role="button"></a></li>
<li><a>15.00</a><a data-icon="delete" data-role="button"></a></li>
<li><a>20.00</a><a data-icon="delete" data-role="button"></a></li>
<li><a>25.00</a><a data-icon="delete" data-role="button"></a></li>
<li><a>50.00</a><a data-icon="delete" data-role="button"></a></li>
<li><a>100.00</a><a data-icon="delete" data-role="button"></a></li>
</ul>
I have a fieldset to add amounts to the list.
<fieldset class="ui-grid-a">
<div class="ui-block-a">
<input type="text" placeholder="Add new Amount" id="contributionAmount" />
</div>
<div class="ui-block-b">
<input type="button" value="Add" id="addContribution"/>
</div>
</fieldset>
I am using the append function to end other amounts that are added to the list. The amount gets added, but the styling (i.e. jquery mobile) classes do not get applied to the new added amount. Can someone tell me on how to overcome the problem.
jQuery is a DOM manipulating/traversing and AJAX JavaScript framework. It abstracts out a lot of the complexity between the different browsers automatically. There are countless jQuery plugins that simplify many task. jQuery Mobile is a UI framework geared to mobile applications that is built on jQuery.
Pages & DialogsA page in jQuery Mobile consists of an element with a data-role="page" attribute. Within the "page" container, any valid HTML markup can be used, but for typical pages in jQuery Mobile, the immediate children of a "page" are divs with data-role="header" , class="ui-content" , and data-role="footer" .
The <select> element creates a drop-down list with several options.
Got it working:
JS
$('.deleteMe').live('click', function(){
$(this).parent().remove();
$('#contributionList').listview('refresh');
});
$('#addContribution').click(function() {
var newAmount = $('#contributionAmount').val();
if(newAmount != '') {
$('#contributionList').append('<li><a>' + newAmount + '</a><a class="deleteMe"></a></li>').listview('refresh');
$('#contributionAmount').val('');
} else {
alert('Nothing to add');
}
});
HTML
<div data-role="page" id="home">
<div data-role="content">
<ul data-role="listview" id="contributionList" data-split-icon="delete" data-split-theme="d">
<li id="l1"><a>5.00</a><a id="1" class="deleteMe"></a></li>
<li><a>10.00</a><a class="deleteMe"></a></li>
<li><a>15.00</a><a class="deleteMe"></a></li>
<li><a>20.00</a><a class="deleteMe"></a></li>
<li><a>25.00</a><a class="deleteMe"></a></li>
<li><a>50.00</a><a class="deleteMe"></a></li>
<li><a>100.00</a><a class="deleteMe"></a></li>
</ul>
<br />
<fieldset class="ui-grid-a">
<div class="ui-block-a">
<input type="text" placeholder="Add new Amount" id="contributionAmount" />
</div>
<div class="ui-block-b">
<input type="button" value="Add" id="addContribution"/>
</div>
</fieldset>
</div>
</div>
You have to refresh the listview
for jQuery Mobile to add the correct classes to the correct elements in your listview
:
$('#addContribution').on('click', function () {
var amount_val = $('#contributionAmount').val();
if (amount_val != '') {
$('#the-listview').append('<li>' + amount_val + '</li>').listview('refresh');
}
});
Here is a demo: http://jsfiddle.net/PQ39n/1/
Docs for jQuery Mobile listview
s: http://jquerymobile.com/demos/1.0/docs/lists/docs-lists.html
EDIT
Phill Pafford brings-up a good point that .on()
is new in jQuery 1.7 and the jQuery Mobile team suggest using jQuery 1.6.4 with jQuery Mobile 1.0. In this case .on()
is the same as using .bind()
so they can be interchanged without issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With