Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select2 -- placeholder not displaying

I'm using Select2 Plugin in my asp.net mvc 5 application. according to the documentation

The placeholder option allows you to pass in a data object instead of just a string if you need more flexibility. The id of the data object should match the value of the placeholder option.

I have done exactly that, but the placeholder is still not showing up.

Code:

model.Step6.Titles.Insert(0, new SelectListItem() { Text = "Select a Title", Value = "0", Selected = true });

@Html.DropDownListFor(model => model.Step6.Title, Model.Step6.Titles, new { id="teamtitle", @style = "width: 100%;"})

$("select").select2({
    allowClear: true,
    placeholder: {
        id: "0",
        placeholder: "Select an Title"
    }
})

can someone show me what I'm doing wrong here?

like image 703
capiono Avatar asked Dec 07 '25 07:12

capiono


2 Answers

I think placeholder is a string. Not an object https://select2.github.io/examples.html#placeholders

$("select").select2({
    allowClear: true,
    placeholder:"Select an Title"

})
like image 55
Srikan Avatar answered Dec 09 '25 21:12

Srikan


Error is placeholder: "Select an Title".It should be-

$("select").select2({
    allowClear: true,
    placeholder: {
        id: "0",
        text: "Select an Title" //Should be text not placeholder
    }
})

https://select2.org/placeholders

like image 34
Zugor Avatar answered Dec 09 '25 19:12

Zugor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!