1

I have the following array:

var sherbert = [{ 
    flavor: "orange mango",
    cost: 2
},{
    flavor: "lemon lime",
    cost: 4
}];

Using jQuery how can I create a function that writes these as LI items like:

<li>flavor : orange mango<span>cost : 2</span></li>
<li>flavor : lemon lime<span>cost : 4</span></li>
3
  • Can't you just iterate the Array and create the elements using the data in each object? Where are you stuck? Commented Apr 17, 2014 at 15:45
  • I keep getting [object Object] returned? Commented Apr 17, 2014 at 15:50
  • 1
    It's always a good idea to include in your question what you've tried so that people can explain specifically what you were doing wrong. Commented Apr 17, 2014 at 15:53

4 Answers 4

3

Something like this should work, I did not test this code.

here are the documentation for jquerys each and append functions:

http://api.jquery.com/each/

http://api.jquery.com/append/

$listSelector = $("#list") //Your list element

$.each(sherbert, function(i, obj) {
    $listSelector.append("<li>flavor : "+obj.flavor+"<span>cost : "+obj.cost+"</span></li>")
});
Sign up to request clarification or add additional context in comments.

Comments

0

Iterate over your sherbert object and add the new HTML as a new li in your desired list.

$.each(sherbert, function(index, dessert) {
    var new_html = "Flavor : " + dessert.flavor + "<span>cost : " + dessert.cost + "</span>";
    $('ul').append($('<li></li>').html(new_html));
});

Comments

0
<script>
var sherbert = [{ 
    flavor: "orange mango",
    cost: 2
},{
    flavor: "lemon lime",
    cost: 4
}];
$(document).ready(function(){
    $(sherbert).each(function(i, el){
        var li = document.createElement('li');
        li.innerHTML = "flavor : " + el.flavor + " <span>cost : " + el.cost + "</span>";
        $("#listItems")[0].appendChild(li);
    });
});
</script>
<body>
    <ul id="listItems">
    </ul>
</body>

2 Comments

sherbert is not a DOM element. the $(selector).each() method is used on DOM elements. You want to use $.each() instead, for iterating over arrays or objects.
While $.each() may be the better approach, $(sherbert).each() certainly works as well. I have found many times when it is nice to use jQuery methods on javascript objects.
0

Here you go:

var ul = $('<ul>');

for (var i = 0; i < sherbet.length; i++ ) {
    var li = $('<li>');
    var item = sherbet[i];
    for(var key in item) {
        var span = $('<span>');
        var text = key + ": " + item[key]; // flavour: orange
        span.html(text); // <span>flavour: orange</span>
        li.append(span); // <li><span>flavour: orange</span><span>cost: 2</span></li>
    }
ul.append(li);
}

Here, you don't really have to hard code any values such as 'flavour' or 'cost'. This code gives you flexibility to alter these texts into something else or add something to it like "size: 10oz" etc.
You just gotta add these additonal values to your array and this code will handle it all!

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.