Button to toggle and display more text | Experience Community
Skip to main content
Solved

Button to toggle and display more text

  • July 19, 2020
  • 40 replies
  • 7103 views

Bharath
Level 1 ●
Forum|alt.badge.img+3

I would like to add a button to display more information if the respondent wants to see. By default the details will be hidden and on clicking the button it should toggle between displaying the details and hiding it again. How can it be done?
I have found the code on how to do it using JS but dont know how to make it work using Jquery.

It is the fourth answer in the link..
Hide and show text by click on div

Best answer by TomG

JS:
jQuery("#button").click(function() {
jQuery("#infodiv").toggle();
});
HTML:


40 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • Answer
  • July 19, 2020

JS:
jQuery("#button").click(function() {
jQuery("#infodiv").toggle();
});
HTML:



Bharath
Level 1 ●
Forum|alt.badge.img+3
  • Author
  • Level 1 ●
  • July 20, 2020

Wow. This worked perfectly. You are awesome TomG. Really appreciate the prompt response. You are incredible!


mklubeck
Level 3 ●●●
Forum|alt.badge.img+8
  • Level 3 ●●●
  • September 2, 2020

Thanks TomG - I had a user want this capability. I searched the community, found this, and it works. NOTE for noobs like me...you need both the JS in the question's JS and the HTML in the question. Change "Button Text" to whatever you want it to say (I like "more/less") and put the text you want to hide / show where the word "Information" is.



Forum|alt.badge.img+1
  • December 15, 2020

Wow, this works really well. Thanks for posting it here.
I know it has been a while but can I ask a total noob question?
Say I wanted to have multiple custom buttons on a page. Let's also say I wanted to measure which of these buttons get clicked (to reveal the text - which varies from button to button) and which do not. Ideally, we would see the order in which they get clicked too...
Do you think that would be possible? If yes, how would I do that?
Many thanks for your advice :)


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • December 16, 2020

https://www.qualtrics.com/community/discussion/comment/32681#Comment_32681Yes, it is possible. You would collect the data needed inside your click event handler and save it to Qualtrics embedded data fields.


  • April 9, 2021

Hi,
Thanks for the code! For me it is not working however. I get a button with 'Button text' as I copy the code in the HTML view, but when I click at it, the hidden text will not show.
Any clue how I can fix this? Thanks in advance :)


  • April 10, 2021

Ah, I did not know that you had to add the JavaScript too, it works now.


  • May 17, 2021

Hi everyone, I am also trying to create a button that can be toggled on/off in the question to show additional information. My problem is the text I want the button to show includes piped text, and I can't find any information on whether it is possible to have piped text inside HTML. Appreciate any advice you have.


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • May 17, 2021

  • May 17, 2021

https://www.qualtrics.com/community/discussion/comment/37415#Comment_37415Hi TomG , thanks for the reply. How exactly can I pipe text into html? When I use the source code editor in the rich content editor, the pipe text button is grayed out. And when I directly type the piped text into the html code it doesn’t work. I’m all out of ideas and would appreciate any explanation. Thanks!


  • May 17, 2021

Sorry just figured out I made a mistake in the piped text! It's working now, thanks very much TomG !


  • May 18, 2021

TomG Hi Tom, I have run into a new problem with my html clickable button. Everything works perfectly when I preview the question, but when I preview block or preview survey, the button doesn't work. I have no idea why this is happening and would really appreciate any advice you have. Thanks!
This is my javascript for the question:
jQuery("#button").click(function() {
  jQuery("#collapseInst").toggle();


});
And this is the html:

 

Click 'Show/hide instructions' if you need to review the instructions again.




 


   





TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • May 18, 2021

Put the JS in the addOnload function.


Forum|alt.badge.img+1
  • September 13, 2021

I'm a total newbie. I think this is the solution to the problem I have - I want more information/text to be visible to people if they click on a word, probably as a pop-up window. I've entered the html and made a button, but nothing happens when I click on it. I can't see a 'cog on the left' (as some have mentioned) in order to do the 'addOnload' - I don't know what this means at all, but gather I need to do this then copy the JS code too? Any help would be very much appreciated! Thanks.


mklubeck
Level 3 ●●●
Forum|alt.badge.img+8
  • Level 3 ●●●
  • September 13, 2021

Lapwing Did you take note of this?
NOTE for noobs like me...you need both the JS in the question's JS and the HTML in the question. Change "Button Text" to whatever you want it to say (I like "more/less") and put the text you want to hide / show where the word "Information" is.


Forum|alt.badge.img+1
  • September 13, 2021

https://community.qualtrics.com/XMcommunity/discussion/comment/40449#Comment_40449Yes but I don't understand how to do the JS - people refer to 'onload' and I don't know what that means. Or do I just paste the JS code and then the html? Do I change anything in the JS code? Thanks


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • September 13, 2021

https://community.qualtrics.com/XMcommunity/discussion/comment/40450#Comment_40450Click on the question the right click on the ... in the upper right, then choose Add JavaScript. Paste the JS code where it says '/*Place your JavaScript here to run when the page loads*/'.
For the html, click on the Question Text then click the HTML View tab. Paste the html were you want the button/info text to appear in the Question Text.


Forum|alt.badge.img+1
  • September 14, 2021

https://community.qualtrics.com/XMcommunity/discussion/comment/40453#Comment_40453Thanks, that helped! However, now nothing appears below the button (or the expanded text if the button is clicked) - what have I done wrong?


Forum|alt.badge.img+1
  • December 17, 2021

Hi,
I was wondering how I can use the button to reveal another answer option in a multiple choice question. Participants for my survey should ideally be able to keep clicking the same button (max. 36x) to display more options if they wish. I've been successful in adding the button so far, do I add to the existing code or would this need a different approach?
Thanks!


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • December 17, 2021

Forum|alt.badge.img+1
  • December 18, 2021

https://community.qualtrics.com/XMcommunity/discussion/comment/42455#Comment_42455Hi Tom, thank you for getting back so quickly. Would you be able to help me with this different code? Or direct me to a page that could? I'm finding it difficult to find.
Thanks again.


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • December 18, 2021

https://community.qualtrics.com/XMcommunity/discussion/comment/42457#Comment_42457I don't know of any code that addresses your specific need. You probably need to write it from scratch.


Forum|alt.badge.img
  • April 27, 2022

Hi there,
I've been following this thread (total newbie here!) and I've followed the instructions to set up a 'more/less' button for questions with details on a participant information sheet they need to read prior to consenting to the study.
I've managed to get it so that when I preview individual questions, each 'more/less' button works, however when I preview it on a page, the clicking the 'more/less' button for the first question (i.e. 'What will happen to me if I take part?' expands with the relevant text - the next questions show on the same page, but none of the more/less buttons work...any ideas? I'm assuming it's how I'm setting up the JS to run as it's only working for the first question and not the others...?
This is the HTML code:

What are the possible pros and cons of taking part?


 



Do I have to take part?


 




And this is the JS:
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
jQuery("#button").click(function() {
  jQuery("#infodiv").toggle();
});

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/

});
  
});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

});
Thanks in advance!


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • April 27, 2022

https://community.qualtrics.com/XMcommunity/discussion/comment/45808#Comment_45808html ids should be unique to the page. Use classes if you want to identify groups of things.


mklubeck
Level 3 ●●●
Forum|alt.badge.img+8
  • Level 3 ●●●
  • May 2, 2022

Hi all (RachDH, uniproject)
sorry I'm late to respond (was on vacation). To the issue about using it on the same page, this is what we tell our users:
NOTE: If you want to use this code more than once in the same block, you need to change the IDs in both the JS and the HTML. For example, change button id = "button" to button id = "button2" and the div id from infodiv to infodiv2 in both the JS and the HTML.