Adding a search Bar | XM Community
Skip to main content
Solved

Adding a search Bar

  • September 20, 2023
  • 10 replies
  • 404 views

Forum|alt.badge.img+4

Hi All,

 

I have added a Pick, Group and Rank type question in my survey but the choices to select from are huge. Is there a way using Javascript where I can add a search bar on the pick box to filter out the choices? 

Best answer by Deepak

@Vinay_Bohra 

Will something like below work for you, add it to the pick, group, rank question.

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
jQuery('.Items h2').append(' <input type="text" id="search-box" placeholder="Search...">');
var items = jQuery('.itemsContainerTd li')
jQuery('#search-box').on('input', function() {
var searchTerm = jQuery(this).val();
jQuery(items).each(function() {
const text = jQuery(this).text().toLowerCase();
if (text.includes(searchTerm)) {
jQuery(this).show();
} else {
jQuery(this).hide();
}
});

});

});

Hope it helps!

10 replies

Nam Nguyen
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+29
  • QPN Level 8 ●●●●●●●●
  • 1096 replies
  • September 20, 2023

@Vinay_Bohra You can check this post for answer
Adding a search bar for choices | XM Community (qualtrics.com)

Hope this helps


Forum|alt.badge.img+4
  • Author
  • QPN Level 2 ●●
  • 10 replies
  • September 20, 2023

@TomG can you help with this?


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 6084 replies
  • September 20, 2023

@Vinay_Bohra,

If I understand correctly, you want a search box that filters the choices so that when you type something it hides all the choices that don’t match the search term.

I think it is possible to do with JS, but it would be quite complicated.


Forum|alt.badge.img+4
  • Author
  • QPN Level 2 ●●
  • 10 replies
  • September 20, 2023

@TomG 
Yes, that is the exact requirement. Could you please provide some guidance around it?


Deepak
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+46
  • 1555 replies
  • Answer
  • September 20, 2023

@Vinay_Bohra 

Will something like below work for you, add it to the pick, group, rank question.

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
jQuery('.Items h2').append(' <input type="text" id="search-box" placeholder="Search...">');
var items = jQuery('.itemsContainerTd li')
jQuery('#search-box').on('input', function() {
var searchTerm = jQuery(this).val();
jQuery(items).each(function() {
const text = jQuery(this).text().toLowerCase();
if (text.includes(searchTerm)) {
jQuery(this).show();
} else {
jQuery(this).hide();
}
});

});

});

Hope it helps!


Forum|alt.badge.img+4
  • Author
  • QPN Level 2 ●●
  • 10 replies
  • September 20, 2023

@Deepak this is really helpful!

I have changed my questions to Multiple Choice now and using following JS for adding search option 

<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>

---- in the look & feel header

var options = {placeholder: 'Select an option'};
jQuery("#"+this.questionId).find('select:first').select2({options});

---- in the question.

 

Thus this solution seems plausible to you or do you have any better suggestion?

 


Deepak
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+46
  • 1555 replies
  • September 20, 2023

@Vinay_Bohra 

If this works for you then that’s great. My code was more specific to Pick Group Rank question type which you have originally asked.

Kindly accept the answer if the code works, as it might be beneficial for others. 


Tom_1842
Level 8 ●●●●●●●●
Forum|alt.badge.img+28
  • Level 8 ●●●●●●●●
  • 909 replies
  • September 20, 2023

@Deepak this is great, thank you! I just have 1 tweak to put the var items =  jQuery('.itemsContainerTd li') underneath the input function. This makes it so that only items that have not yet been put into a Group will be filtered. When it is above, the items that have been placed in Groups are also filtered. So updated JS below:

jQuery('.Items h2').append(' <input type="text" id="search-box" placeholder="Search...">');
jQuery('#search-box').on('input', function() {
var items = jQuery('.itemsContainerTd li');
var searchTerm = jQuery(this).val();
jQuery(items).each(function() {
const text = jQuery(this).text().toLowerCase();
if (text.includes(searchTerm)) {
jQuery(this).show();
} else {
jQuery(this).hide();
}
});
});

 


Deepak
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+46
  • 1555 replies
  • September 20, 2023

@Tom_1842 

That’s a good tweak I just thought that if the choices are large and if a person forgets if something has already been grouped seeing that explicitly in grouped choices would clear the doubt instead of scrolling the group.

But yeah, thanks for the tweak!


Tom_1842
Level 8 ●●●●●●●●
Forum|alt.badge.img+28
  • Level 8 ●●●●●●●●
  • 909 replies
  • September 20, 2023

Ahh that makes sense. Works well!