using switch function multiple times

JazzAviator 1 Reputation point
2022-08-04T15:28:00.007+00:00

I have a custom list form with a bunch of fields. many feilds are hidden on load and will populate as the user selects certain options. i'm running in to an issue though. I have a drop down with a bunch of options. when a user selects an option, it displays their choice and then an additional dropdown selection with the same options. The problem I run in to is that if a user selects an option on the second dropdown, it populates the new field, but if they change their selection in the same field, it populates the next selection but doesn't remove their first selection. If I attempt to hide the previous, then it also deletes the results of the first selection. Here's my code and some images to help you understand (I'm only including two instances, but there are 5 total dropdowns):

$("select[title='Warfare Qualification']").change(function() {

	$('nobr:contains("Date awarded")').closest('tr').hide();   
		switch($(this).val()) {  
		  
		case "[Select One]":  
			$('nobr:contains("2nd Warfare Qualification")').closest('tr').hide();  
		break;  
		case "AW":  
			$("span[id='AW1']").closest('tr').show();  
			$('nobr:contains("2nd Warfare Qualification")').closest('tr').show();  
		break;  
		case "CAC":  
			$("span[id='CAC1']").closest('tr').show();  
			$('nobr:contains("2nd Warfare Qualification")').closest('tr').show();  
		break;  
		case "DSW":  
			$("span[id='DSW1']").closest('tr').show();  
			$('nobr:contains("2nd Warfare Qualification")').closest('tr').show();  
		break;  
		case "DV":  
			$("span[id='DV1']").closest('tr').show();  
			$('nobr:contains("2nd Warfare Qualification")').closest('tr').show();  
		break;  
		case "EWS":  
			$("span[id='EWS1']").closest('tr').show();  
			$('nobr:contains("2nd Warfare Qualification")').closest('tr').show();  
		break;  
		case "EXW":  
			$("span[id='EXW1']").closest('tr').show();  
			$('nobr:contains("2nd Warfare Qualification")').closest('tr').show();  
		break;  
		case "FMF":  
			$("span[id='FMF1']").closest('tr').show();  
			$('nobr:contains("2nd Warfare Qualification")').closest('tr').show();  
		break;  
		case "FPJ":  
			$("span[id='FPJ1']").closest('tr').show();  
			$('nobr:contains("2nd Warfare Qualification")').closest('tr').show();  
		break;  
		case "IW":  
			$("span[id='IW1']").closest('tr').show();  
			$('nobr:contains("2nd Warfare Qualification")').closest('tr').show();  
		break;  
		case "NAC":  
			$("span[id='NAC1']").closest('tr').show();  
			$('nobr:contains("2nd Warfare Qualification")').closest('tr').show();  
		break;  
		case "PJ":  
			$("span[id='PJ1']").closest('tr').show();  
			$('nobr:contains("2nd Warfare Qualification")').closest('tr').show();  
		break;  
		case "SCW":  
			$("span[id='SCW1']").closest('tr').show();  
			$('nobr:contains("2nd Warfare Qualification")').closest('tr').show();  
		break;  
		case "SEAL":  
			$("span[id='SEAL1']").closest('tr').show();  
			$('nobr:contains("2nd Warfare Qualification")').closest('tr').show();  
		break;  
		case "SG":  
			$("span[id='SG1']").closest('tr').show();  
			$('nobr:contains("2nd Warfare Qualification")').closest('tr').show();  
		break;  
		case "SS":  
			$("span[id='SS1']").closest('tr').show();  
			$('nobr:contains("2nd Warfare Qualification")').closest('tr').show();  
		break;  
		case "SW":  
			$("span[id='SW1']").closest('tr').show();  
			$('nobr:contains("2nd Warfare Qualification")').closest('tr').show();  
		break;  
		case "SWCC":  
			$("span[id='SWCC1']").closest('tr').show();  
			$('nobr:contains("2nd Warfare Qualification")').closest('tr').show();  
		break;  
		  
		default:  
			$('nobr:contains("Date awarded")').closest('tr').hide();  
			$('nobr:contains("2nd Warfare Qualification")').closest('tr').hide();  
			  
	}  

});  
$("select[title='2nd Warfare Qualification']").change(function() {  
		  
	switch($(this).val()) {  
		  
		case "[Select One]":  
			$('nobr:contains("3rd Warfare Qualification")').closest('tr').hide();  
		break;  
		case "AW":  
			$("span[id='AW1']").closest('tr').show();  
			$('nobr:contains("3rd Warfare Qualification")').closest('tr').show();  
		break;  
		case "CAC":  
			$("span[id='CAC1']").closest('tr').show();  
			$('nobr:contains("3rd Warfare Qualification")').closest('tr').show();  
		break;  
		case "DSW":  
			$("span[id='DSW1']").closest('tr').show();  
			$('nobr:contains("3rd Warfare Qualification")').closest('tr').show();  
		break;  
		case "DV":  
			$("span[id='DV1']").closest('tr').show();  
			$('nobr:contains("3rd Warfare Qualification")').closest('tr').show();  
		break;  
		case "EWS":  
			$("span[id='EWS1']").closest('tr').show();  
			$('nobr:contains("3rd Warfare Qualification")').closest('tr').show();  
		break;  
		case "EXW":  
			$("span[id='EXW1']").closest('tr').show();  
			$('nobr:contains("3rd Warfare Qualification")').closest('tr').show();  
		break;  
		case "FMF":  
			$("span[id='FMF1']").closest('tr').show();  
			$('nobr:contains("3rd Warfare Qualification")').closest('tr').show();  
		break;  
		case "FPJ":  
			$("span[id='FPJ1']").closest('tr').show();  
			$('nobr:contains("3rd Warfare Qualification")').closest('tr').show();  
		break;  
		case "IW":  
			$("span[id='IW1']").closest('tr').show();  
			$('nobr:contains("3rd Warfare Qualification")').closest('tr').show();  
		break;  
		case "NAC":  
			$("span[id='NAC1']").closest('tr').show();  
			$('nobr:contains("3rd Warfare Qualification")').closest('tr').show();  
		break;  
		case "PJ":  
			$("span[id='PJ1']").closest('tr').show();  
			$('nobr:contains("3rd Warfare Qualification")').closest('tr').show();  
		break;  
		case "SCW":  
			$("span[id='SCW1']").closest('tr').show();  
			$('nobr:contains("3rd Warfare Qualification")').closest('tr').show();  
		break;  
		case "SEAL":  
			$("span[id='SEAL1']").closest('tr').show();  
			$('nobr:contains("3rd Warfare Qualification")').closest('tr').show();  
		break;  
		case "SG":  
			$("span[id='SG1']").closest('tr').show();  
			$('nobr:contains("3rd Warfare Qualification")').closest('tr').show();  
		break;  
		case "SS":  
			$("span[id='SS1']").closest('tr').show();  
			$('nobr:contains("3rd Warfare Qualification")').closest('tr').show();  
		break;  
		case "SW":  
			$("span[id='SW1']").closest('tr').show();  
			$('nobr:contains("3rd Warfare Qualification")').closest('tr').show();  
		break;  
		case "SWCC":  
			$("span[id='SWCC1']").closest('tr').show();  
			$('nobr:contains("3rd Warfare Qualification")').closest('tr').show();  
		break;  
		  
		default:  
			$('nobr:contains("Date awarded")').closest('tr').hide();  
			$('nobr:contains("3rd Warfare Qualification")').closest('tr').hide();  
			  
	}  

});  

When the page loads, and after the user makes it to this point, the "Warfare Qualification" dropdown exists.
228160-render.png

When they select an option, the appropriate action displays.
228222-render2.png

When they select the 2nd option, the appropriate action displays.
228197-render3.png

However, if a user changes their selection in the "2nd..." dropdown, the date awarded option remains while selecting the next one.
228126-render4.png

What am I missing here?

JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
866 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,668 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,116 Reputation points
    2022-08-05T09:38:31.867+00:00

    Hi @JazzAviator ,

    To help you better, my understanding: when the 2nd option DV is selected **,the **date awarded DV will display, When 2nd option selection is changed to FPJ ** , the **date awarded DV is hidden and the date awarded FPJ is displayed. If my understanding is incorrect, please feel free to contact me.

    According to my research , you can hide the date awarded DV elements in the code case "FPJ": . For example:

    case "FPJ":  
                 $("span[id='DV1']").closest('tr').hide();  
                 $("span[id='FPJ1']").closest('tr').show();  
                 $('nobr:contains("3rd Warfare Qualification")').closest('tr').show();  
             break;  
    

    Hope it can help you. Thanks for your understanding.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



  2. Tong Zhang_MSFT 9,116 Reputation points
    2022-08-19T02:00:42.227+00:00

    Hi @JazzAviator ,

    I'm glad to hear you solve the problem ,if you have any issue about SharePoint, you are welcome to raise a ticket in this forum.

    By the way, since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others." and according to the scenario introduced here: Answering your own questions on Microsoft Q&A, I would make a brief summary of this thread:

    [using switch function multiple times]

    Issue Symptom:

    I have a custom list form with a bunch of fields. many feilds are hidden on load and will populate as the user selects certain options. i'm running in to an issue though. I have a drop down with a bunch of options. when a user selects an option, it displays their choice and then an additional dropdown selection with the same options. The problem I run in to is that if a user selects an option on the second dropdown, it populates the new field, but if they change their selection in the same field, it populates the next selection but doesn't remove their first selection. If I attempt to hide the previous, then it also deletes the results of the first selection.

    Current status:

    I scrapped the switch function and just wrote it the long way with a crap ton of "if" statements.

    You could click the "Accept Answer" button for this summary to close this thread, and this can make it easier for other community member's to see the useful information when reading this thread. Thanks for your understanding!


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    0 comments No comments