Quantcast
Viewing all articles
Browse latest Browse all 6517

New Post: Show/Hide form selections based on drop down selection possible?

I was able to achieve my request through the following code.

If there is a way to improve the code, please let me know.

Thanks!
//Variables
//FieldNameToHideShow equates to the field you don’t want visible unless a certain option is selected from another drop down
//FieldNameToSelect is the field when changed will determine whether to show or hide the FieldNameToHide

//Syntax to identify the field row to hide
//$('h3:contains("FieldNameToHideShow")').closest('tr').hide();
//$('h3:contains("FieldNameToHideShow")').closest('tr').show();

//Syntax to hide/show field row based on another fields selection
//$("select[title='FieldNameToSelect']").click(function() {
//  if ($("select[title='FieldNameToSelect'] option:selected").text()=="CriteriaGoesHere"){
//    //Hide/Show
//  } else { 
//    //Hide/Show
//  } 
//});

So for my example the code would look like

//Initialize - Hide all non-required data entries
$('h3:contains("Month")').closest('tr').hide();
$('h3:contains("Year")').closest('tr').hide();
$('h3:contains("Owner")').closest('tr').hide();

//Handle Category drop down
$("select[title='Category']").click(function() {
  //Handle report selection
  if ($("select[title='Category'] option:selected").text()=="Report"){
    $('h3:contains("Month")').closest('tr').show();
    $('h3:contains("Year")').closest('tr').show();
    $('h3:contains("Owner")').closest('tr').hide();
  } else { 
    //Default to null/none/blank
    $("select[title='Month']").get(0).selectedIndex = 0
    $("select[title='Year']").get(0).selectedIndex = 0
    //Hide
    $('h3:contains("Month")').closest('tr').hide();
    $('h3:contains("Year")').closest('tr').hide();
  } 
  //Handle documentation selection
  if ($("select[title='Category'] option:selected").text()=="Documentation"){
    $('h3:contains("Month")').closest('tr').hide();
    $('h3:contains("Year")').closest('tr').hide();
    $('h3:contains("Owner")').closest('tr').show();
  } else { 
    //Default to null/none/blank
    $("select[title='Owner']").get(0).selectedIndex = 0
    //Hide
    $('h3:contains("Owner")').closest('tr').hide();
  } 
});

Viewing all articles
Browse latest Browse all 6517

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>