0

I have a javascript file that when called, checks to see if a particular option is selected on a form. The form allows for multiple selections before being submitted. When a particular item is selected within the given choices it shows a hidden menu. In this case with "audits" I am able to show the hidden menu fine when just "audits" is selected from the list. However, I'm having much difficulty in figuring out how to get the menu to show when "audits" would be selected/highlighted with others. Eg: I had audits, services, someotheroption Below you can see the code I'm currently using and that's working only when the single item is selected. Any guidance would be much appreciated.

function toggleFields(){
    function toggleFields(){
        if ($("#installations").val() == "audits"){
            $("#dbcredentialsfield").show();
        }
        else
            $("#dbcredentialsfield").hide();
}
Stan
  • 3
  • 1
  • 2
    Could you create a jsfiddle or code snippet with your entire project? Welcome to Stackoverflow. – www139 Dec 23 '15 at 00:44
  • I would be more than happy to help out if you have a JSfiddle of your code! – realappie Dec 23 '15 at 00:45
  • 1
    Possible duplicate of [How to get multiple select box values using jquery?](http://stackoverflow.com/questions/3243476/how-to-get-multiple-select-box-values-using-jquery) – Sean Dec 23 '15 at 00:50
  • You can use [`$.inArray()`](https://api.jquery.com/jQuery.inArray/). ie. `if(isArray($("#installations").val()) && $.inArray("audits", $("#installations").val()){ ...` – Sean Dec 23 '15 at 01:00
  • Here is the link to the jsfiddle. Also, I had to remove the following out of the body in the html code section. I was having trouble figuring out a workaround. https://jsfiddle.net/b3ojbghm/ – Stan Dec 24 '15 at 03:53

1 Answers1

0

Using the code you have so far, I assume you probably want something like this:

$('#installations').on('change', function(){
   $("#dbcredentialsfield").toggle($(this).val() == 'audits');
});

This says; when the select element (assuming your dropdown has the id of installations) changes, toggle the visibility of the element with id dbcredentialsfield depending on if the value of the select is audits or not.

Donnie D'Amato
  • 3,832
  • 1
  • 15
  • 40
  • Tried this option including it in the code but wasn't able to get it to work. I've attached a jsfiddle up above since others were asking. Thank you for the explanation. Definitely new at this and actually being described to the process of things is a tremendous help. – Stan Dec 24 '15 at 03:52