How to generate UL Li list from string array using jquery?
Asked Answered
K

7

48

I have string array like

'United States', 'Canada', 'Argentina', 'Armenia', 'Aruba', 'Australia', 'Austria', 'Azerbaijan', 'Bahamas', 'Bangladesh', 'Belarus', 'Belgium'**, ... etc.

I want create a dynamic list from string array like below:-

<ul class="mylist" style="z-index: 1; top: 474px; left: 228px; display: none; width: 324px;" > 
  <li class="ui-menu-item" role="menuitem">
     <a class="ui-all" tabindex="-1">
         United States
    </a>
  </li>
  <li class="ui-menu-item" role="menuitem">
     <a class="ui-all" tabindex="-1">
         Canada
    </a>
  </li>
  <li> .... </li>
   .....
</ul>

How is it possible using jQuery?

Knecht answered 4/5, 2011 at 9:4 Comment(0)
F
95
var countries = ['United States', 'Canada', 'Argentina', 'Armenia'];
var cList = $('ul.mylist')
$.each(countries, function(i)
{
    var li = $('<li/>')
        .addClass('ui-menu-item')
        .attr('role', 'menuitem')
        .appendTo(cList);
    var aaa = $('<a/>')
        .addClass('ui-all')
        .text(countries[i])
        .appendTo(li);
});
Fabiano answered 4/5, 2011 at 9:43 Comment(1)
What the difference here to using vanilla js's .forEach() ?There
N
20

With ES6 you can write this:

const countries = ['United States', 'Canada', 'Argentina', 'Armenia'];

const $ul = $('<ul>', { class: "mylist" }).append(
  countries.map(country => 
    $("<li>").append($("<a>").text(country))
  )
);
Nickelsen answered 17/12, 2015 at 13:5 Comment(1)
you suggest also works. thanks for result in short line. how can add new class "mylist" to UL listKnecht
H
14

An even better approach using the array's join method

const countries = ['United States', 'Canada', 'Argentina', 'Armenia'];
const list = '<ul class="myList"><li class="ui-menu-item" role="menuitem"><a class="ui-all" tabindex="-1">' + countries.join('</a></li><li class="ui-menu-item" role="menuitem"><a class="ui-all" tabindex="-1">') + '</a></li></ul>';
Henbane answered 15/3, 2015 at 18:40 Comment(2)
if by better you mean shorter, then yes better. but not as simple to comprehend.There
The opening <a> and closing </a> tags are unmatched. You need an opening <a ...> in the join() parameter.Obligee
T
6
var countries = ['United States', 'Canada', 'Argentina', 'Armenia'];
var cList = $('ul.mylist')
$.each(countries, function(i) {
    var li = $('<li/>')
        .addClass('ui-menu-item')
        .attr('role', 'menuitem')
        .appendTo(cList);
    var a = $('<a/>')
        .addClass('ui-all')
        .text( this )
        .appendTo(li);
});
Tabanid answered 4/5, 2011 at 9:14 Comment(2)
Thanks , I getting error message a is undefined in jquery javascript, I am using jquery 1.5.1. How to fixed it?Knecht
Switch over to the development version of jQuery. That'll show you where it's failing.Tabanid
F
4
    <script type="text/javascript" >
        function aa()
        {
            var YourArray = ['United States', 'Canada', 'Argentina', 'Armenia'];
            var ObjUl = $('<ul></ul>');
            for (i = 0; i < YourArray.length; i++)
            {
                var Objli = $('<li></li>');
                var Obja = $('<a></a>');

                ObjUl.addClass("ui-menu-item");
                ObjUl.attr("role", "menuitem");

                Obja.addClass("ui-all");
                Obja.attr("tabindex", "-1");

                Obja.text(YourArray[i]);
                Objli.append(Obja);

                ObjUl.append(Objli);
            }
            $('.DivSai').append(ObjUl);
        }
    </script>
</head>
<body onload="aa()">
    <form id="form1" runat="server">
    <div class="DivSai" >

    </div>
    </form>
</body>
Fabiano answered 4/5, 2011 at 9:19 Comment(0)
U
2

Other variation of Abhishek Bhalani: You can use Array.map() instead of $.each()

var items = ['United States', 'Canada', 'Argentina', 'Armenia'];
var cList = $('ul.mylist');
items.map( (item,i ) => {
      var li = $('<li/>')
        .addClass('ui-menu-item')
        .attr('role', 'menuitem')
        .appendTo(cList);
      $('<a class="ui-all">'+ i + ': ' + item.name + '<a/>')
        .appendTo(li);
    });
Ultramundane answered 27/4, 2016 at 10:24 Comment(0)
Z
0

It's possible without jQuery too! :)

let countries = ['United States', 'Canada', 'Argentina', 'Armenia', 'Aruba'];

// The <ul> that we will add <li> elements to:
let myList = document.querySelector('ul.mylist');

// Loop over the Array of country names:
countries.forEach(function(value, index, array) {
  // Create an <li> element:
  let li = document.createElement('li');

  // Give it the desired classes & attributes:
  li.classList.add('ui-menu-item');
  li.setAttribute('role', 'menuitem');

  // Now create an <a> element:
  let a = document.createElement('a');

  // Give it the desired classes & attributes:
  a.classList.add('ui-all');
  a.tabIndex = -1;
  a.innerText = value; //  <--- the country name from our Array
  a.href = "#"

  // Now add the <a> to the <li>, and add the <li> to the <ul>
  li.appendChild(a);
  myList.appendChild(li);
});
<ul class="mylist"></ul>
Zymometer answered 24/6, 2019 at 2:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.