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.
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.
Share Improve this question asked Dec 15, 2011 at 18:31 HozefaHozefa 1,7266 gold badges22 silver badges35 bronze badges 02 Answers
Reset to default 12Got it working:
- http://jsfiddle.net/NXrRp/10/
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.