0

i have a button and when i click this button i want a select option added. I can add select but not options, why? I want it added according to the list. Not adding 'foreach loop option'

enter image description here

$(function () {
                var items = ["fa fa-address-book", "fa fa-address-book-o", "fa fa-address-card", "fa fa-address-card-o"];
                $('#add-menu').on('click', function (e) {
                    $('#menu').append('<li>\n' +
                        '<div class="row">\n' +
                        '<label class="col-sm-2 col-form-label">Social Media İcon</label>\n' +
                        '<div class="col-sm-8">\n' +
                        ' <div class="form-group bmd-form-group" id="icon">\n' +
                        '<select class="form-control selectpicker" id="mySelect" name="icon[]" title="Social Media icon" data-live-search="true" ' +
                        'data-hide-disabled="true">\n' +
                        $.each(items, function (i, item) {
                            $('#mySelect').append($('<option>', {
                                value: item.value,
                                text: item.text
                            }));
                        }),
                        '</select>\n' +
                        '</div>\n' +
                        '</div>\n' +
                        '</div>\n' +
                        '</div>' +
                        '</li>');
                    e.preventDefault();
                });
            });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="menus">
    <ul id="menu" class="menu">
    <li></li>
    </ul>
    </div>
    <a href="#" id="add-menu" class="btn">add-Menu</a>

8
  • $.each(items, function (i, item) -> items is not defined in your code snippit... Is that coming from code you have not included in your question? Commented Aug 15, 2020 at 23:53
  • I do not understand what you mean Commented Aug 16, 2020 at 0:09
  • Added Snippet.. Commented Aug 16, 2020 at 0:15
  • can you add a sample of your desired outcome ? Commented Aug 16, 2020 at 0:16
  • If you check spinnet, option does not come Commented Aug 16, 2020 at 0:17

1 Answer 1

2

You're quite close; just replace $.each() with:

items.map(item => `<option>${item}</option>`).join('') +

as in this demo:

$(function () {
                var items = ["fa fa-address-book", "fa fa-address-book-o", "fa fa-address-card", "fa fa-address-card-o"];
                $('#add-menu').on('click', function (e) {
                    $('#menu').append('<li>\n' +
                        '<div class="row">\n' +
                        '<label class="col-sm-2 col-form-label">Social Media İcon</label>\n' +
                        '<div class="col-sm-8">\n' +
                        ' <div class="form-group bmd-form-group" id="icon">\n' +
                        '<select class="form-control selectpicker" id="mySelect" name="icon[]" title="Social Media icon" data-live-search="true" ' +
                        'data-hide-disabled="true">\n' +
                        items.map(item => `<option>${item}</option>`).join('') +
                        '</select>\n' +
                        '</div>\n' +
                        '</div>\n' +
                        '</div>\n' +
                        '</div>' +
                        '</li>');
                    e.preventDefault();
                });
            });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="menus">
    <ul id="menu" class="menu">
    <li></li>
    </ul>
    </div>
    <a href="#" id="add-menu" class="btn">add-Menu</a>

NOTE

If you intend to add several select tags using the button id="mySelect" will be quite problematic in that you'll end up with more than one element having the same id attribute value. You don't want to end up there.

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

4 Comments

i know off topic but can i write items items php function?
I would not advise using PHP to create JS. It's better to put the data in a data attribute and JS can read it from there on the client side.
var items = [<?php foreach (icons() as $id => $icons): ?>"<?= $icons ?>",<?php endforeach; ?>]; I ran it this way, it worked, do you mind?
Like I said, not a good idea. Ideally, ALL your JavaScript should be in a completely separate file.

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.