select2: cannot read property 'id' of undefined
Asked Answered
F

2

36

I used Select2 4.0.6, I have a bug like below:

allowClear gives "Uncaught TypeError: Cannot read property 'id' of undefined" when used on select.

How can I fix this bug?.

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
</head>
<body>
    <select role="select" id="myoption">
        <option value="001">abcs </option><option value="002">dshdsh</option><option value="003">A</option>
        <option value="004">ANAM CO</option>
    </select>
</body>
</html>
<script>
    $("#myoption").select2({
        allowClear: true,
        width: '300px',
        height: '34px'
        //data: data
    });
</script>
Frumpish answered 21/4, 2018 at 7:32 Comment(2)
I don't see any error from your snippet.Stays
Click the x in the select and you'll see the error.Eggert
F
75

If you set the debug property to true you'll see a warning message.

The allowClear option should be used in combination with the placeholder option.

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
</head>
<body>
    <select role="select" id="myoption">
        <option value="001">abcs </option><option value="002">dshdsh</option><option value="003">A</option>
        <option value="004">ANAM CO</option>
    </select>

</body>
</html>
<script>
 $("#myoption").select2({
        allowClear: true,
        width: '300px',
        height: '34px',
        debug: true
        //data: data
    });
</script>

So you have to define a placeholder:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
</head>
<body>
    <select role="select" id="myoption">
        <option value="001">abcs </option><option value="002">dshdsh</option><option value="003">A</option>
        <option value="004">ANAM CO</option>
    </select>

</body>
</html>
<script>
 $("#myoption").select2({
        allowClear: true,
        width: '300px',
        height: '34px',
        placeholder: 'select..'
        //data: data
    });
</script>
Film answered 21/4, 2018 at 7:37 Comment(2)
"The allowClear option should be used in combination with the placeholder option." - it so nice...Greenleaf
That solved my case too. I think select 2 might include that info in documentation.Overstudy
C
4

In my case data-placeholder helped as shown below:

Select element:

<select class="form-control select2" data-placeholder="All" required="required" id="manager_id" name="filter[manager_id]" tabindex="-1" aria-hidden="true">
    <option value="1" selected="selected">Manager 1</option>
    <option value="2" selected="selected">Manager 2</option>
</select>

JS:

$('.select2').each(function(){
    var select = $(this);
    select.select2({
         width: '100%',
         allowClear: !select.prop('required'),
         language: {
            noResults: function () {
                return "{{ __('No results found') }}";
            }
        }
    });
});
Cheree answered 2/12, 2019 at 16:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.