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

Select2 doesn't work with multiple <select> with same id #5501

Closed
maxrd2 opened this issue May 4, 2019 · 12 comments
Closed

Select2 doesn't work with multiple <select> with same id #5501

maxrd2 opened this issue May 4, 2019 · 12 comments

Comments

@maxrd2
Copy link

maxrd2 commented May 4, 2019

Select2 doesn't convert multiple <select> elements that share same id attribute (I know that id attribute must be unique).
This used to work just fine in 4.0.5.
Since 4.0.6 it doesn't work anymore and just one element out of many is "upgraded" to select2.

Have tried this on firefox, didn't try other browsers.

Example is here, changing script version to 4.0.5 will make it work fine:
http://jsbin.com/bejobisavo/edit?html,js,output

@vinaychetu
Copy link

Yes, Same issue is there in the example website with me also.
Even Unable to select or close select box for Ajax(Remote Data - https://select2.org/data-sources/ajax) Example.

May this issue/bug is only with the latest(v4.0.6). Earlier is working great.

@kevin-brown
Copy link
Member

I know that id attribute must be unique

Bingo. We officially do not support this use case because it breaks a lot of the way we need to generate internal IDs.

With that being said, if you are able to trace through the differences between 4.0.5 and 4.0.6, I'm willing to entertain a pull request that fixes this as long as it doesn't break anything else.

@vinaychetu
Copy link

vinaychetu commented May 6, 2019

Hmm. In my case I'm not using multiple same/unique id attribute. Still ajax(remote data) thing not working correctly. Check example data on your website. select box is not closing after option selection.

Even this is not with Ajax part most of yours examples in each section not working as above mentioned.

Its also described in Issue #5500

@kevin-brown
Copy link
Member

@vinaychetu that issue is different from the one reported here. That issue is captured and fixed in #5490.

This issue is still present in 4.0.7-rc.0.

@onet4
Copy link

onet4 commented May 30, 2019

I have a similar problem with v4.0.6 or above. Even when the element is given a different ID, if the data-select2-id value is the same, the problem occurs. This is problematic when cloning elements to implement repeatable form fields.

Here is an example.

<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <link href="https://rawgit.com/select2/select2/4.0.6/dist/css/select2.min.css" rel="stylesheet">        
        <script src="https://rawgit.com/select2/select2/4.0.6/dist/js/select2.full.min.js"></script>
        <style>
            #container > div {
                margin: 2em;
            }
        </style>
    </head>
    <body>    
        <h1>Clone Element Test</h1>
        <div id='container'>
            <h2>Original</h2>
            <select id="test-clone-select-01">
                <option>January</option>
                <option>February</option>
                <option>March</option>
            </select>         
        </div>
    </body>
    <script>
        $( document ).ready( function(){

            var options = {};

            $( '#test-clone-select-01' ).select2( options );
            var target_element = $( '#test-clone-select-01' );
            var cloned_element = target_element.clone();

            cloned_element.attr( 'id', 'test-clone-select-02' );
            // cloned_element.attr( 'data-select2-id', '' );
            
            cloned_element.removeClass( 'select2-hidden-accessible' );
             $( '#container' ).append( '<hr><h2>Cloned</h2>' );
             $( '#container' ).append( cloned_element );
            cloned_element.select2( options );
            
        });

    </script>
</html>

Change the line https://rawgit.com/select2/select2/4.0.6/dist/js/select2.full.min.js to https://rawgit.com/select2/select2/4.0.5/dist/js/select2.full.min.js and the problem does not occur.

A workaround is to remove the data-select2-id value of the subject select element. I guess this can be natively supported as it can be avoided simply adding element.attr( 'data-select2-id', '' );.

@rohanc
Copy link

rohanc commented Jun 9, 2019

The workaround suggested by @onet4 works better if you also remove the data-select2-id from the child elements. Otherwise the cloned menu remains entangled with the original menu, which is evident from the previously-selected option highlighting being incorrect.

@kimek
Copy link

kimek commented Jul 18, 2019

@kevin-brown "Bingo. We officially do not support this use case because it breaks a lot of the way we need to generate internal IDs."
We understand but this is breaking change for a lot of people.
This should be 4.1.0, not 4.0.6 ;(

@kevin-brown
Copy link
Member

We continue to believe that this was not specified or documented behaviour, since it violates expectations that the id attribute of an element is unique within a document.

The id global attribute defines an identifier (ID) which must be unique in the whole document. Its purpose is to identify the element when linking (using a fragment identifier), scripting, or styling

https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id
https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute

The workaround suggested by @onet4 works better if you also remove the data-select2-id from the child elements. Otherwise the cloned menu remains entangled with the original menu, which is evident from the previously-selected option highlighting being incorrect.

This is being fixed in #5587 and is actually a different issue from the one mentioned here. We have fixed the issue that some people were seeing when closing a destroyed Select2 and will be releasing the fix in the next version.

@dios1101
Copy link

dios1101 commented Aug 2, 2019

Is it possible to add an error in the console to explains that existing select2 will be destroyed (span select2) to avoid a lot a time consuming research? Because this error will still hapening normaly.

@stale
Copy link

stale bot commented Oct 1, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the status: stale label Oct 1, 2019
@stale stale bot closed this as completed Oct 8, 2019
@meduzapat
Copy link

BTW duplicated Ids are not allowed by HTML:

The id global attribute defines an identifier (ID) which must be unique in the whole document. Its purpose is to identify the element when linking (using a fragment identifier), scripting, or styling (with CSS).

@gitanupam
Copy link

Adding to what @rohanc said above. Best is to remove the data-select2-id instead of setting it to blank since multiple elements with blank data-select2-id also don't seem to work with select2. Something like this:

$("#"+num+"_form").find("select").removeAttr( 'data-select2-id');

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

No branches or pull requests

9 participants