0

i have some data into sort array i want get each value from array in to Select option value. how can i get values from Array and make output like HTML? I would be grateful for any help.

JS

    $(document).ready(function () {
        new Sortable(drop, {
            onChange: function (event, ui) {
                sort = ["67", "66", "65", "64", "63"];
                $('#drop').children().each(function () {
                    $('#id_articles').append(`<option value="${sort}" selected=""></option>`);
                    sort.push($(this).data('pk'))
                    console.log(sort)
                });
            },
        })
    }); 

HTML

<select name="articles" required="" id="id_articles" multiple>
    <option value="67" selected=""></option>
    <option value="66" selected=""></option>
    <option value="65" selected=""></option>
    <option value="64" selected=""></option>
    <option value="63" selected=""></option>
</select>
2
  • You want to generate the HTML you wrote? Commented Jan 15, 2021 at 0:59
  • What is the situation at the beginning, what is the situation at the end and what causes the change? Commented Jan 15, 2021 at 1:00

1 Answer 1

1

Here is the solution of appending options into select box...

sort = ["67", "66", "65", "64", "63"];
$.each(sort, function (index, value) {
    $('#id_articles').append($('<option/>', { 
        value: value,
        text : value 
    }));
}); 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="articles" required="" id="id_articles" multiple>
</select>

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

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.