Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read property 'className' of undefined #2092

Closed
Mobe91 opened this issue Sep 6, 2018 · 12 comments
Closed

Cannot read property 'className' of undefined #2092

Mobe91 opened this issue Sep 6, 2018 · 12 comments
Labels

Comments

@Mobe91
Copy link
Contributor

Mobe91 commented Sep 6, 2018

When I create a select control without any options and with width: auto, I get the following error during initialization:

Uncaught TypeError: Cannot read property 'className' of undefined
    at Selectpicker.liHeight (bootstrap-select.js:1474)
    at eval (bootstrap-select.js:1716)

When I remove the width: auto option the initialization succeeds but I get the following error when I click on the select control:

bootstrap-select.js:1474 Uncaught TypeError: Cannot read property 'className' of undefined
    at Selectpicker.liHeight (bootstrap-select.js:1474)
    at Selectpicker.setSize (bootstrap-select.js:1672)
    at HTMLButtonElement.eval (bootstrap-select.js:1994)
    at HTMLButtonElement.dispatch (jquery.js:5183)
    at HTMLButtonElement.elemData.handle (jquery.js:4991)

I am using bootstrap-select v1.13.2

@caseyjhol
Copy link
Member

Bug reports must include a live demo of the problem. Per our contributing guidelines, please create a reduced test case via Plunker, jsFiddle, CodePen or JS Bin, and report back with your link, Bootstrap version, bootstrap-select version, and specific browser and OS details.

@Mobe91
Copy link
Contributor Author

Mobe91 commented Sep 7, 2018

Here is the live demo: https://plnkr.co/edit/xR4pMYKoFwYxXV6B2t5A?p=preview
Bootstrap version: 4.1.0
jQuery version: 3.2.1
bootstrap-select version: 1.13.2
Tested with browsers Firefox and Chrome on Windows 10

Mobe91 added a commit to ordami/bootstrap-select that referenced this issue Sep 7, 2018
@Mobe91
Copy link
Contributor Author

Mobe91 commented Sep 7, 2018

@caseyjhol Oh you have already fixed that. Nice! 👍

@kriit24
Copy link

kriit24 commented Sep 20, 2018

in bootsrap-select.js line 1063 i change the code
if (this.options.title && !this.multiple) {
TO
if (this.options.title /&& !this.multiple/) {

and now it works, but a am sure that "!this.multiple" is needed

@robertpustulka
Copy link

@caseyjhol Hello, thanks for the fix. Can you tag this fix?

@cubitworx
Copy link

I am experiencing the same problem. It occurs in v1.13.2 but not in v1.13.1 so I have downgraded for now. This happens when there are no options in the select element. In my case I was able to narrow the offending line down to the following:

a.className = 'dropdown-item ' + this.$element.find('option')[0].className;

Where className does not exist on this.$element.find('option')[0] because there are no options and index 0 is undefined.

@redbonzai
Copy link

redbonzai commented Oct 10, 2018

I am getting the same error. But in my case the select already has options, and I am checking whether the select contains particular options. If it does, then I am forcing the option to toggle selected, and then I am refreshing the select with this : '$('.selectpicker').selectpicker('refresh');`
That is where the error is thrown.

Here is my code:

 var modal = $("div#editUserActivity");
    modal.find("input[name$=activity_name]").attr('value', data.activity.activity_name);
    modal.find('#activity_description').text(data.activity.activity_description);

$.each( data.activity.focuses, function(index, value) {
         if (modal.find('#activity_focus option[value="'+ value.id +'"]').length !== 0) {
                modal.find('#activity_focus').prop('selected', true).val(value.id);
        } else {
                modal.find('#activity_focus').append($('<option />').val(value.id).text(value.primary_focus).attr('selected', 'selected'));
       }

        modal.find('.selectpicker').selectpicker('refresh'); <-- where error is thrown.
 });

The error thrown:

app.js?id=289c53d38b0c16fad0d1:3502 Uncaught TypeError: Cannot read property 'className' of undefined
    at Selectpicker.liHeight (app.js?id=289c53d38b0c16fad0d1:3502)
    at Selectpicker.setSize (app.js?id=289c53d38b0c16fad0d1:3700)
    at Selectpicker.refresh (app.js?id=289c53d38b0c16fad0d1:4633)
    at HTMLSelectElement.<anonymous> (app.js?id=289c53d38b0c16fad0d1:4722)
    at Function.each (app.js?id=289c53d38b0c16fad0d1:25190)
    at jQuery.fn.init.each (app.js?id=289c53d38b0c16fad0d1:25025)
    at jQuery.fn.init.Plugin [as selectpicker] (app.js?id=289c53d38b0c16fad0d1:4702)
    at Object.<anonymous> (activities.js?id=4e199cc85f13e6c4bd44:278)
    at Function.each (app.js?id=289c53d38b0c16fad0d1:25190)
    at Object.<anonymous> (activities.js?id=4e199cc85f13e6c4bd44:271)

@redbonzai
Copy link

It looks like if I access selectpicker by the the id selector instead of by the class the error is no longer emitted.
So in this case : modal.find('#activity_focus').selectpicker('refresh');

This seems to solve my problem.
I don't know what this says about other probably reasons for this error though.

Thank you for your time.

Christian

@mdjaman
Copy link

mdjaman commented Oct 10, 2018

No fix at all!

@yurik94
Copy link

yurik94 commented Oct 12, 2018

Commit: 62a3eac fixes the problem but it should be published a new version!

@caseyjhol
Copy link
Member

Released in v1.13.3!

@siva-gembrill
Copy link

I also received same issue in bootstrap file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants