2

I am working on cordova app .I want to create list items using jquery . here is my html code :

 <div class="body" id="list">
        <ul class="list list-messages" id="list">


        </ul>
      </div>

And this is my js code :

    <script>
        $(document).ready(function () {
            $(".navbar-title").html(localStorage.getItem("Provider"));

        $.ajax({
            type: 'Get',
            url: 'http://41.128.183.109:9090/api/data/getalllocations',
            success: function (data) {

            for (var i = 0; i < 11; i++) {

$("#list").append('<li class="list-message" data-ix="list-item"><a class="w-clearfix w-inline-block" href="chat.html" data-load="1"><div class="w-clearfix column-left"><div class="image-message"><img src="images/128.jpg"></div></div><div class="column-right"><div class="message-title">James White</div><div class="message-text">Hey dude! We are waiting for you at the main station, we will meet you near to....</div></div></a></li>');

                }

            }
        });
        });
    </script>

Its not working . please advice

2
  • 1
    use append instead of html() Commented Jan 21, 2016 at 11:26
  • innerHtml is a javascript property. and it gives html value and you can set html value. But for pushing html you can use append of jQuery Commented Jan 21, 2016 at 11:31

4 Answers 4

3

You should use .append() instead of .html()

.html() is replacing the content of your div, .append() will add some content

for (var i = 0; i < 11; i++) {
    $("#list").append('<li class="list-message" data-ix="list-item"><a class="w-clearfix w-inline-block" href="chat.html" data-load="1"><div class="w-clearfix column-left"><div class="image-message"><img src="images/128.jpg"></div></div><div class="column-right"><div class="message-title">James White</div><div class="message-text">Hey dude! We are waiting for you at the main station, we will meet you near to....</div></div></a></li>');
}
Sign up to request clarification or add additional context in comments.

2 Comments

yes it solved but what is the different between html and innerhtml and append ? thank you
.html() is a jQuery method, it sets the HTML of your div ; .innerHTML is a vanilla javascript property, you can set its value with item.innerHTML = 'xxx' ; .append() is a jQuery method, it will add some content to you div
1

You should append() not html()

Html() replaces value and append() inserting value.

 $(document).ready(function () {

                $("#list").empty();
                for (var i = 0; i < 11; i++) {
                    $("#list").append('<li class="list-message" data-ix="list-item"><a class="w-clearfix w-inline-block" href="chat.html" data-load="1"><div class="w-clearfix column-left"><div class="image-message"><img src="images/128.jpg"></div></div><div class="column-right"><div class="message-title">James White</div><div class="message-text">Hey dude! We are waiting for you at the main station, we will meet you near to....</div></div></a></li>');

                }    
        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="body" id="list">
        <ul class="list list-messages" id="list">


        </ul>
      </div>

4 Comments

innerHtml is a javascript property. and it gives html value and you can set html value. But for pushing html you can use append of jQuery
yes checked. as you have make answer of other person but check to do $("#list").empty(); before append. Ex. If your ajax return 0 data then you need to .empty() on success first. On html it should have zero li not previous list items
Thanks for making it answer.
i do this but it seems that it get data but it isn't appear . i dont know why
0
    <html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>

<script>

    $(document).ready(function () {
        for (var i = 0; i < 11; i++) {
            $("#list").append('<li class="list-message" data-ix="list-item"><a class="w-clearfix w-inline-block" href="chat.html" data-load="1"><div class="w-clearfix column-left"><div class="image-message"><img src="images/128.jpg"></div></div><div class="column-right"><div class="message-title">James White</div><div class="message-text">Hey dude! We are waiting for you at the main station, we will meet you near to....</div></div></a></li>');

        }

    });
</script>
</head>
<body>
   <div class="body" id="list">
        <ul class="list list-messages" id="Ul1">
        </ul>
      </div>
</body>
</html>

Comments

0

I solve this problem by delete "data-ix="list-item" Now My Code is : $("#list").append('James WhiteHey dude! We are waiting for you at the main station, we will meet you near to....'); I dont Know Why But it is running now .

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.