0
votes

I am using out of the box Sharepoint 2013 to create a custom list. On the NewForm.aspx, I have created code within a Content Editor Web Part that should show or hide columns based on another column value (Invocation / Near Miss?). This works fine. I've duplicated this function to do something similar based on the value of another column (called 'Which GCC / MIG / IMT?') but this one is not working.

Please note that the 'Which GCC / MIG / IMT?' column is a lookup from another sharepoint list I have. it contains approx. 90 entries. Those entries, they have text in there similar to the below (a small extract for context purposes):

Europe-Czech Republic-MIG

Europe-EU-MIG

Europe-EU-IMT-GBM

Europe-France-MIG

Europe-France-IMT-GBM

Essentially, I want to tweak my original code (that works) so that it looks at the value in the 'Which GCC / MIG / IMT?' column and if the value selected contains the letters 'IMT' (ie include some wildcards) then show a column called 'IMT PIR'. However, it only "hides" the column and doesn't "show" the column when I do finally select a value that has the characters "IMT" within it.

Unfortunately, other than searching various forums to understand how wildcards work within Java, i'm not too sure where to go. I don't know for example, if I can have multiple functions within the same script. is this possible?

So the following function works fine:

<script src="http://code.jquery.com/jquery-1.7.2.min.js" type="text/javascript&quot;"></script><script type="text/javascript">

$(document).ready(function(){

  // hide various columns when selecting "Near Miss" value 

  $("select[title='Invocation / Near Miss?']").change(function() {
    if ($("select[title='Invocation / Near Miss?']").val() == "Near Miss") {
        $('nobr:contains("Date of first IM meeting")').closest('tr').hide();
        $('nobr:contains("Date of last IM meeting")').closest('tr').hide();
        $('nobr:contains("Number of meetings?")').closest('tr').hide();
        $('nobr:contains("PIR Author")').closest('tr').hide();
        $('nobr:contains("PIR Approval Date")').closest('tr').hide();
        $('nobr:contains("PIR Submitted to Corporate Security Site?")').closest('tr').hide();
        $('nobr:contains("HELIOS Event ID")').closest('tr').hide();
        $('nobr:contains("Incident status")').closest('tr').hide();
        $('nobr:contains("PIR attached?")').closest('tr').hide();
        $('nobr:contains("Incident date (near miss)")').closest('tr').show();
       } 
  // Show certain columns when selecting "Invocation"

    else if($("select[title=' Invocation / Near Miss?']").val() !== "Invocation"){
        $('nobr:contains("Date of first IM meeting")').closest('tr').show();
        $('nobr:contains("Date of last IM meeting")').closest('tr').show();
        $('nobr:contains("PIR Author")').closest('tr').show();
        $('nobr:contains("PIR Approval Date")').closest('tr').show();
        $('nobr:contains("PIR Submitted to Corporate Security Site?")').closest('tr').show();
        $('nobr:contains("HELIOS Event ID ")').closest('tr').show();
        $('nobr:contains("Number of meetings?")').closest('tr').show();
        $('nobr:contains("HELIOS Event ID")').closest('tr').show();
        $('nobr:contains("Incident status")').closest('tr').show();
        $('nobr:contains("PIR attached?")').closest('tr').show();
        $('nobr:contains("Incident date (near miss)")').closest('tr').hide();
      }
 });
});

This next function (which is coded within the same CEWP) just hides the IMT PIR column but regardless of which value I choose and will not show it again if I select a value that has "IMT" within the value.

$(document).ready(function(){

  // Show IMT PIR column when selecting a value which has "IMT" in it"
  $("select[title='Which GCC / MIG / IMT?']").change(function() {
    if ($("select[title='Which GCC / MIG / IMT?']").val() == "*IMT*") {
        $('nobr:contains("IMT PIR")').closest('tr').show();

       } 
   // Hide IMT PIR column when NOT selecting a value which has "IMT" in it"
    else if($("select[title='Which GCC / MIG / IMT?']").val() !== "*IMT*"){
        $('nobr:contains("IMT PIR")').closest('tr').hide();
      }
 });
});

 </script> 

In essence, I would expect to see the IMT PIR column appear where I've highlighted in yellow, when a value within the 'Which GCC / MIG / IMT?' column has a value containing 'IMT' within it; is selected.

Please note, I'm not a developer but would really appreciate some guidance from you amazing people :)

Thanks Sean Example of what I'm expecting to see

1

1 Answers

0
votes

Use the $(this) keyword since you are already inside the change function.

$(document).ready(function(){

  // Show IMT PIR column when selecting a value which has "IMT" in it"
  $("select[title='Which GCC / MIG / IMT?']").change(function() {
    if ($(this).val() == "*IMT*") {
        $('nobr:contains("IMT PIR")').closest('tr').show();

       } 
   // Hide IMT PIR column when NOT selecting a value which has "IMT" in it"
    else if($(this).val() !== "*IMT*"){
        $('nobr:contains("IMT PIR")').closest('tr').hide();
      }
 });
});