Hi! I’m trying to create a menu from an xml file, via jquery.
I need to run the script for this, on window-load.
It’s a dropdown menu with some jquery code to it in my plugins.js
I’m trying to completely add all the items of the xml to the UL of the menu list class…
Here’s the code in which I need to add my menu to:
<li class="w-nav-item level_1 has_sublevel">
<a class="w-nav-anchor level_1" href="#">
<span class="w-nav-icon"><i class="fa fa-files-o"></i></span>
<span class="w-nav-title">Animals</span>
<span class="w-nav-arrow"></span>
</a>
<!-- level 2 -->
<ul class="w-nav-list level_2">
<!---------------1 START animals with sublevel---------------->
<li class="w-nav-item level_2 has_sublevel">
<a class="w-nav-anchor level_2" href="javascript:void(0);">
<span class="w-nav-title">Birds*</span>
<span class="w-nav-arrow"></span>
</a>
<!-- level 3 -->
<ul class="w-nav-list level_3">
<li class="w-nav-item level_3">
<a class="w-nav-anchor level_3" href="animals/1_sparrow.html">
<span class="w-nav-title">Sparrow</span>
<span class="w-nav-arrow"></span>
</a>
</li>
<li class="w-nav-item level_3">
<a class="w-nav-anchor level_3" href="animals/1_crow.html">
<span class="w-nav-title">Crow</span>
<span class="w-nav-arrow"></span>
</a>
</li>
</ul>
</li>
<!---------------END animals with sublevel------------------>
<!---------------2 START animals with sublevel---------------->
<li class="w-nav-item level_2 has_sublevel">
<a class="w-nav-anchor level_2" href="javascript:void(0);">
<span class="w-nav-title">Carnivorous Animals</span>
<span class="w-nav-arrow"></span>
</a>
<!-- level 3 -->
<ul class="w-nav-list level_3">
<li class="w-nav-item level_3">
<a class="w-nav-anchor level_3" href="animals/2_tiger.html">
<span class="w-nav-title">Tiger</span>
<span class="w-nav-arrow"></span>
</a>
</li>
</ul>
</li>
<!---------------END animals with sublevel------------------>
<!---------------3 START animals with sublevel---------------->
</li>
<!---------------END animals with sublevel------------------>
And here’s the XML From which I need to create the menu…:
<items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<item>
<product_id>1_1</product_id>
<product_title>Sparrow*</product_title>
<product_generic>A Sparrow Bird</product_generic>
<product_category>Birds*</product_category>
<product_code>Code 152</product_code>
<product_img>images_animals/1_1_sparrow.png</product_img>
<product_url>1_sparrow.html</product_url>
</item>
<item>
<product_id>1_2</product_id>
<product_title>Crow</product_title>
<product_generic>A Crow Bird</product_generic>
<product_category>Birds*</product_category>
<product_code>Code 153</product_code>
<product_img>images_animals/1_2_crow.png</product_img>
<product_url>1_crow.html</product_url>
</item>
<item>
<product_id>2_1</product_id>
<product_title>Tiger*</product_title>
<product_generic>A Wild Tiger</product_generic>
<product_category>Carnivorous Animals </product_category>
<product_code>Code 151</product_code>
<product_img>images_animals/2_1_tiger.png</product_img>
<product_url>2_tiger.html</product_url>
</item>
...
...
...
I’m trying to append all the “items” in the xml to the menu, as per the div format of li list. After the "
<ul class="w-nav-list level_2">
"
I have this code, that I was using to add “countries” to in my contact form, but I don’t know how to change it to add the required divs.
var cList = $('ul.w-nav-list.level_2')
$.each(data, function(i)
{
var cat_no = data.id.split('_');
var opt = $('<option/>')
.attr('value', countries[i])
.text(countries[i])
.appendTo(cList);
});
I have the XML id in the format of:
1_1: first item, category 1,
1_2: second item, category 1,
1_3: third item, category, 1,
2_1: fourth item, category 2,
3_1: fifth item, category 3,
… like this there are about 10-13 categories with about 100 xml items.
I believe something of this sort should do:
ajax: xml_url, etc
xml response, data ,
data.xml_id.split('_'); //to get the first element of the xml id
function add_menu(){
$('ul.w-nav-list level_2').append(data, function ( ){ for(i=0;i<=data.length) for all data.xml_id==i: add category + item divs, li, ul, /li, /ul) }
The above is just a kindof alogrithm or a way of how I think this can be done… I don’t know how to do this in jquery/javascript.
Please could someone provide help regarding this?