4

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

The problem of course is that the code doesnt go through each list item and output it in its own option tag.

2 Answers 2

3
$("li").each(function () {
    $('<option />').text($(this).text())
                   .val($(this).text())
                   .appendTo("select");    
});

Your fiddle, re-fiddlified.

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

1 Comment

B CUZ U alrdy optmiz 4 me. KTHXBYE. :)
0

Don't need jQuery. Just create a new option node for each element and append that to the select.

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

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.