Reputation: 16428
I'm trying to dynamically add items into a <ul>
via jquery, and have followed documentation here
I seem to be struggling with the listview
invocation...
This is my HTML :
<ul data-role="listview" id="tweets">
<li><a href="astra.html">Astra</a></li>
<li><a href="ford.html">Ford</a></li>
<li><a href="triumph.html">Triump</a></li>
<li><a href="chevrolet.html">Chevrolet</a></li>
<li><a href="dodge.html">Dodge</a></li>
</ul>
This is my Jquery:
$("#tweets").append("<li>Nissan</li>");
$("#tweets").listview("refresh");
And this is the error I see in Chrome JS console:
Uncaught TypeError: Object [object Object] has no method 'listview'
This is what I see in the browser :
I've tried calling listview both with and without the "refresh", both seem to give the same error, what am I doing wrong?
I've looked at ~5 previous SO questions, but none of their answers have helped.
EDIT:
I already have the following in the <head>
elements :
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>
EDIT 2
The above javascript is being execute after a button is clicked, not sure if that has any effect...
HTML:
<a href="#" data-role="button" onclick="getTweets()">GetTweets</a>
JS:
function getTweets() {
// some static value for now, to get it working...
$("#tweets").append("<li>Nissan</li>");
try {
$('#tweets').listview('refresh');
} catch(e) {
$('#tweets').listview();
}
}
Upvotes: 2
Views: 1944
Reputation: 3583
Your error is that jQuery Mobile is not defined. is it included before the jQuery? Do you overrun jQuery somehow? I'll need more code in order to help you with this problem.
Upvotes: 0
Reputation: 673
I'm guessing your listview is not initiated the first time you add an item. Try this, for example:
try {
$('#tweets').listview('refresh');
} catch(e) {
$('#tweets').listview();
}
Upvotes: 1