Home > OS >  Select 2 fails to work, Cant pinpoint the reason
Select 2 fails to work, Cant pinpoint the reason

Time:10-13

i've got a razor page, With the following :

@section renderHead{
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>    
}

The Dropdown :

<div id="multiplySelect" class="col-lg-9 col-md-9 col-sm-12">
            @if (Enumerable.Count(ViewBag.WriterList) == 0)
            {
                @Html.DropDownList("multipleWriters", (IEnumerable<SelectListItem>)ViewBag.WriterList, new { @class = "form-control", multiple = "multiple", disabled = "disabled" })
                <label id="error" class="form_label_comment">אין קטגוריות קיימות.</label>
            }
            else
            {
                @Html.DropDownList("multipleWriters", (IEnumerable<SelectListItem>)ViewBag.WriterList, new { @class = "form-control", data_val = "false", multiple = "multiple", style = "width:100%;" })
            }
        </div>

And the Select2 Execution (inside a script tag ofcourse):

$('multipleWriters').select2({
        language: "he",
        allowClear: true,
        placeholder: {
            "id": "",
            "text": ''
        },
    });

A bit lost, At the moment i get an open dropdown which looks nothing like Select2

CodePudding user response:

the selector $('multipleWriters') is wrong

To select by id Must be (note the # in front)

$('#multipleWriters')

or to select by class $('.classname') if you use class name (note the dot . in front)

  • Related