0

I'm trying to add an li to a ul using jquery and can't get it to work. I am using the following code.

I have read an XML file which is in the following format for example,

<Products>
    <Description>Pound</Description>
    <ProductType>CUR</ProductType>
    <CurrencyCode>GBP</CurrencyCode>
    <Rate>1</Rate></ProductRate>
    <Description>Euro</Description>
    <ProductType>CUR</ProductType>
    <CurrencyCode>Eur</CurrencyCode>
    <Rate>1.5</Rate></ProductRate>
</Products>

I am trying to use jquery to create a list of the products but it keeps adding them as 1 li element and not multiple li's where list is the actual list,

I am using the following jquery

var $xml = $(xml);
var $list = $('#list');
$description = $xml.find("Description");
$( "#list" ).append($description.text());`

This reads the elements but adds them as a single li element

<ul>
  <li>
    Pound GBP Euro Eur
  </li>
</ul> 

I have also tried

$( "#list" ).append("<li>"+$description.text()+"</li>"); 

but this still delivers the same html. So my question is how do I ensure they are shown as separate li elements?

2 Answers 2

3

Try with

$description.each(function() {
    $("#list").append("<li>" + $(this).text() + "</li>");
});

DEMO

Sign up to request clarification or add additional context in comments.

1 Comment

using the same XML is it possible within the each function to grab the 'rate' by doing something like $rate = $xml.find("Rate"); $( "#list" ).append($rate.text());
2

Try to iterate through each of the elements being read:

$xml.find("Description").each(function(){
     $( "#list" ).append("<li>"+$(this).text()+"</li>");
});

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.