Есть ли способ создать неупорядоченный список HTML из массива в Javascript? - PullRequest
0 голосов
/ 05 апреля 2019

Я хочу использовать чистый html и javascript вместо HAML.Раньше, когда я работал с этим проектом формы, я перебирал массив таким образом

- @item1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
- item1.each_with_index do |item, i|
   %li.option
      %input.option-input{name: 'options', type: 'radio', value: i, id: "options-#{i}"}/
      %label.option-label{:for => "options-#{i}"}= item1

1 Ответ

0 голосов
/ 05 апреля 2019
<script>
    (function(){
        const items = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
        const ul = document.createElement('UL');
        for (let i=0; i< items.length; i++){
            const item = items[i];
            const li = document.createElement('LI');
            const label = document.createElement('LABEL');
            const input = document.createElement('INPUT');
            input.value = item;
            input.setAttribute('id', 'options-' + i);
            input.setAttribute('type', 'radio');
            input.setAttribute('name', 'options');
            label.appendChild(input);
            label.setAttribute('for', 'options-' + i);
            label.appendChild(document.createTextNode(item));
            li.appendChild(label);
            ul.appendChild(li);
        }
        document.body.appendChild(ul);
    })();
</script>
...