2

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 :

enter image description here

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();
                }
}
7
  • Have you included jQuery Mobile correctly? Your error suggests that it hasn't been loaded. Commented Jul 30, 2012 at 13:41
  • Check that you include the jquery mobile and write your code within $(document).bind('pageinit'). Commented Jul 30, 2012 at 13:42
  • I've already included the jquery script references :S Commented Jul 30, 2012 at 13:45
  • @thecodeparadox Don't use document.ready(), use $(document).bind('pageinit') instead jquerymobile.com/demos/1.1.1/docs/api/events.html Commented Jul 30, 2012 at 13:46
  • Your missing the a tag from your append, other then that your code seems to be correct. Commented Jul 30, 2012 at 13:50

2 Answers 2

1

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();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but unfortunately that still gives the same behaviour.. :S
=/ I guess you're doing it too soon somehow. Change the document.ready() for $(document).bind('pageinit') or other variant recommended at jquerymobile
0

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.

Comments

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.