Support

Home Forums Event Espresso Premium JQuery Snippet to Hide Question Based upon Logic

JQuery Snippet to Hide Question Based upon Logic

Posted: November 6, 2018 at 8:43 am


dangbird

November 6, 2018 at 8:43 am

Hello Team – I attempted to follow the post regarding hiding questions based upon previous question on Registration form and failed miserably. Basically I have TWO Dropdowns, ID 13 and ID 14. IF ID 13 Dropdown is changed from No To YES, I want to SHOW ID 14 etc. I am using EE4 and latest updates.

I know I need to add a Function/Snippet but am a tiny bit ignorant as to how the JQUERY is done etc.


Josh

  • Support Staff

November 6, 2018 at 8:48 am

Hi,

Can you post a link to an event on your site that uses the custom registration form so we can take a look at what’s going on there?


dangbird

November 6, 2018 at 8:52 am

https://training.paulmitchell.edu/eventinfo/texture/

Start Registration, then on the Form the question is:
“Attending 3rd Day Certification?”
“Select Certifying Level” should hide unless YES is selected for “Attending 3rd Day Certification?”

Thanks Josh!


Josh

  • Support Staff

November 6, 2018 at 10:59 am

Hi,

I checked the registration form and I don’t see the custom script loading anywhere. Has that been removed? If it has been removed, can you share the code you’re working with into a PasteBin or GitHub Gist, then share that link here?


dangbird

November 6, 2018 at 11:34 am

Hi Josh – This is the code and obviously not working/correct.
I think you wrote this for EE3 not EE4, and it was for a text field.
Also I am using a Plugin Called “Snippets” to load the code easier.

//* Please do NOT include the opening php tag, except of course if you’re starting with a blank file
// change 105 to match your text field question ID
// change 104 to match your dropdown select question ID
function ee_custom_show_question_conditionally() {
wp_add_inline_script(
‘validation’,
‘jQuery( document ).ready(function($) {
var text = $(“#ee-single-page-checkout-dv div[id*=\’ee_reg_qstn-27-13\’]”);
$(text).hide();
$(“.ee_reg_qstn-27-14”).change(function() {
$(this).val() == “other” ?
$(text).show() :
$(text).hide();
});
});’
);
}
add_action( ‘wp_enqueue_scripts’, ‘ee_custom_show_question_conditionally’, 11 );


Josh

  • Support Staff

November 6, 2018 at 11:49 am

For EE4 you can follow this example instead:

https://gist.github.com/joshfeck/183209d5239cc07ec94f9ebbfb017d40#file-functions-php

In that example you’ll note that it’s adding to a different script (one that’s actually used in EE4) and that it’s using classes for selectors instead of IDs.


dangbird

November 6, 2018 at 12:58 pm

Hi Josh – Thanks for your help this worked great.
To save you and others time, I made some code changes to your snippet and added detailed instructions as most of us are not as Sharp as you!
I also made it so the Question shows if they change back, and hid the entire DIV of the question. I hope this contributes.
I am all set thank you again!
**********************************************

/* Please do NOT include the opening php tag, except of course if you’re starting with a blank file

To hide or Show an Entire Question (Conditional Logic) follow these steps:

1. Build Questions In EE4 Registration Forms.
2. Add to Question Group.
3. For event in question open the form (Go thru registration so you see the form live)
4. Identify the Logic question – (The Question that will Determine if the next question will show)
5. In Google Chrome Browser Right Click right on the Question and hit INSPECT
6. Look for the CSS ID of the Select Box, Check Box, etc. It will be in the form: ee_reg_qstn-XX-YY where XX is the Group, and YY is the Question ID (USE THE CSS ID “#” NOT CLASS “.”
7. In this example the *Dropdown* question div class is #ee_reg_qstn-32-14
8. The next step is to identify the DIV CLASS (The entire Div you want to hide) in the form.
9. In Google Chrome Browser Right Click on the Question to hide and hit INSPECT
10. In this example the DIV CLASS is .ee-reg-qstn.ee-reg-qstn-13-input-dv.ee-select-input-dv (USE THE CSS CLASS “.” not ID “#”
11. The value to evaluate is the #ee_reg_qstn-XX-YY value in this example it was “Yes”

*/

function ee_custom_show_question_conditionally() {
wp_add_inline_script(
‘ee_form_section_validation’,
‘jQuery( document ).ready(function($) {
var text = $(“.ee-reg-qstn.ee-reg-qstn-13-input-dv.ee-select-input-dv”);
$(text).hide();
$(“#ee_reg_qstn-32-14”).change(function() {
//Uncomment Below to Test value of Question so you can put in the IF statement “Yes” in this example
//var test=$(this).val();
//Uncomment Below to Show the Value to the browser
//alert(test);
$(this).val() == “Yes” ?
$(text).show() : $(text).hide();
});
});’
);
}
add_action(‘wp_enqueue_scripts’,’ee_custom_show_question_conditionally’, 11);


Josh

  • Support Staff

November 6, 2018 at 1:16 pm

Thanks for sharing. Usually it’s best to post larger blocks of code into a PasteBin or GitHub gist to preserve the code format (especially important since single and double quotes get transformed into curly quotes by the forum software)

Also, you’ll note you can get the ID of the questions from viewing the list table in Event Espresso > Registration Form > Questions.


dangbird

November 7, 2018 at 7:46 am

Hopefully this is cleaner
<script src=”https://gist.github.com/dangbird/23829b1da91741c0b678bd0de147de05.js”></script>

The support post ‘JQuery Snippet to Hide Question Based upon Logic’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso