Parse the html list with javascript, then print it down

I am trying to write javascript that will automatically accept the text in the html ul list and then output it as drop down. Here is what I still have: http://jsfiddle.net/KRWHP/

The problem, of course, is that the code does not go through each element of the list and displays it in its own parameter tag.

+4
source share
2 answers
$("li").each(function () { $('<option />').text($(this).text()) .val($(this).text()) .appendTo("select"); }); 

Your violin is paraphrased.

+3
source

No need jQuery. Just create a new node option for each element and add it to the selection.

 var ul = document.getElementsByTagName("ul")[0]; var select = document.getElementsByTagName("select")[0]; [].forEach.call(ul.children, function (el) { var option = document.createElement("option"); option.textContent = el.textContent; select.appendChild(option); }); 

Example

0
source

Source: https://habr.com/ru/post/1392062/


All Articles