Recent searches


No recent searches

How to read a drop-down value on a custom form in Javascript?



Posted Mar 20, 2024

I'm trying to hide the Attachments options on a custom request form in Guide when a custom field dropdown is set to No, and leave the Attachments fields alone when it's set to Yes or not set. I've got the JS code to hide the Attachments bits, but I can't for the life of me figure out the correct if statement.

Fair warning, I'm not really familiar with JS, but I'm not new to coding. I've tried variations on the following, with no luck.:

if (document.querySelector('.request_custom_fields_24800067478036').value= "[custom field tag or custom field value]")

and

if ($('.request_custom_fields_24800067478036').value= "[custom field tag or custom field value]")

Any help is appreciated.

EDIT: I was able to find someone doing something similar, and found code that lead to this. Ideally I'd only need the first if, but looks like if they change between the two options, unless I have the second if the attachments won't come back. If anyone knows a better way to do this, please let me know, but this is working now.

$('#request_custom_fields_24800067478036').change(function(){
if ($(this).attr('value') == 'ecno_vetted_no') {
$('label[for=request-attachments]').hide();
  $('#upload-dropzone').hide();
}
});
$('#request_custom_fields_24800067478036').change(function(){
if ($(this).attr('value') == 'ecno_vetted_yes') {
  $('label[for=request-attachments]').show();
  $('#upload-dropzone').show();
}
});

0

3

3 comments

image avatar

Jakub

Zendesk Customer Care

Hello Kyle Kowalsky

From what I understand with the approach in the edit, you're able to hide the attachment field when your custom field is marked, but it does not go back when the end user changes it to YES. 

The mutation observer probably should be applied to monitor those changes in the custom field and show/hide the element accordingly. Is your Help Center private or public? If it is public, feel free to share the URL, so I can run some tests and see if it is something I can help with. 

Thank you

0


Jakub Thanks for the reply! Our Help Center is private, but the edit actually has it working the way I need it. When the user first opens the form, Attachments is there, if they set the dropdown to No, Attachments gets hidden, and if they set the dropdown (back) to Yes, the Attachments makes sure that it's shown.

It's probably not the most effective or efficient method, but I'm alright with it for now.

0


image avatar

Jakub

Zendesk Customer Care

Ah! Now I understand that you would like to initially hide it and then control it by your custom field. In case you need any help moving forward, let me know, have a great day!

0


Please sign in to leave a comment.

Didn't find what you're looking for?

New post