Select2 plugin is crashing | XM Community
Question

Select2 plugin is crashing

  • 26 April 2024
  • 1 reply
  • 15 views

Userlevel 1
Badge +1

We are trying to embed select2 in a survey to enable users to keyword search through and select a large, but finite, number of possible text responses. specifically, we are trying to pre-load roughly ~27000 job titles into a survey question where respondents are asked to enter their job title and then select from a drop-down menu that appears with a manageable sub-set of possible options. So, we’re hoping to provide things like: 

  1. elementary teacher
  2. secondary school teacher
  3. music teacher
  4. kindergarten teacher 

and then have each of those return to the search string “teacher” and enable the respondent to then pick the one that is closest to their occupation. You can see the proof of concept working very well here: 

https://wlu.yul1.qualtrics.com/jfe/preview/previewId/7af0c4bb-0bee-43b0-bbac-31c7f2436b03/SV_9El6TuOelduuINU?Q_CHL=preview&Q_SurveyVersionID=current

 

A shortened version of the javascript code is here:

 Qualtrics.SurveyEngine.addOnReady(function() {

        var occupations = [

{"id": "1", "text": "Aboriginal band chief"}, {"id": "2", "text": "Aboriginal band council member"}, {"id": "3", "text": "Alderman/woman"}, {"id": "4", "text": "Attorney general"}, {"id": "5", "text": "Cabinet minister"}, {"id": "6", "text": "City alderman"}, {"id": "7", "text": "City councillor"}, {"id": "8", "text": "City councilman/woman"}, {"id": "9", "text": "Councillor"}, {"id": "10", "text": "First Nations band chief"}, {"id": "11", "text": "First Nations band council member"}, {"id": "12", "text": "First Nations government leader"}, {"id": "13", "text": "Government leader"}, {"id": "14", "text": "Governor general"}, 

        ];

jQuery("#"+this.questionId+" select").select2({

placeholder: 'select your occupation',

    data: occupations

});

});

 

One thought we had would be: is there a way for javascript to draw the possible responses from a text file rather than be built into the Javascript command itself? Sorry, my knowledge of javascript is *extremely* limited. 

Thank you!


1 reply

Userlevel 7
Badge +27

You could use the built-in select2 ajax functionality, but you need a web server to host a server side script and the data (preferably in a database).

Leave a Reply