Skip to content
Advertisement

jQuery show/hide based on select (survey mode)

I want to improve the code offered here https://jsfiddle.net/comprido/dcgjzfph/4/ based in the infographic time: What’s the best T-shirt printing technique for me just to pass that questions to my clients before they choose a printing option. I’m sure the coding can improve… any hand?

Based on the article show/hide div based on select option jquery

  $(function() {
        $('#select1').change(function(){
            $('#' + $(this).val()).show();
			$("option:selected").removeAttr("selected");
			$('#question1').hide();
        });
		 $('#select2').change(function(){
            $('#' + $(this).val()).show();
			$("option:selected").removeAttr("selected");
            $('#question2').hide();
        });
		 $('#select3').change(function(){
            $('#' + $(this).val()).show();
			$("option:selected").removeAttr("selected");
            $('#question3').hide();
        });
		 $('#select4').change(function(){
            $('#' + $(this).val()).show();
			$("option:selected").removeAttr("selected");
            $('#question4').hide();
        });
		 $('#select5').change(function(){
            $('#' + $(this).val()).show();
			$("option:selected").removeAttr("selected");
            $('#question5').hide();
        });
		 $('#select6').change(function(){
            $('#' + $(this).val()).show();
			$("option:selected").removeAttr("selected");
            $('#question6').hide();
        });
		 $('#select7').change(function(){
            $('#' + $(this).val()).show();
			$("option:selected").removeAttr("selected");
             $('#question7').hide();
       });
	    $('#select11').change(function(){
            $('#' + $(this).val()).show();
			$("option:selected").removeAttr("selected");
             $('#question11').hide();
       });
	    
    });
	function showDiv() {
   document.getElementById('question1').style.display = "block";
   $('#question2').hide();
   $('#question3').hide();
   $('#question4').hide();
   $('#question5').hide();
   $('#question6').hide();
   $('#question7').hide();
   $('#question8').hide();
   $('#question9').hide();
   $('#question10').hide();
   $('#question11').hide();
   $('#question12').hide();
   $('#question13').hide();
}
/* Some basic page styles */
body {
  font: 100%/1.5 AvenirNext-Regular, Corbel, "Lucida Grande", "Trebuchet Ms", sans-serif;
  color: #111; 
  background-color: #fff;
  margin: 2em 10%
}
h1,h2,h3 { color:#000}
/* Label styles: style as needed */
label {
  display:block;
  margin: 2em 1em .25em .75em;
  font-size: 1.25em;
  color:#333;
}

/* Container used for styling the custom select, the buttom class adds the bg gradient, corners, etc. */
.dropdown {
  position: relative;
  display:block;
  margin-top:0.5em;
  padding:0px;
}

/* This is the native select, we're making everything the text invisible so we can see the button styles in the wrapper */
.dropdown select {
  width:100%;
  margin:0;
  background:none;
  border: 1px solid transparent;
  outline: none;
  /* Prefixed box-sizing rules necessary for older browsers */
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  /* Remove select styling */
  appearance: none;
  -webkit-appearance: none;
  /* Magic font size number to prevent iOS text zoom */
  font-size:1.25em;
  /* General select styles: change as needed */
  /* font-weight: bold; */
  color: #444;
  padding: .6em 1.9em .5em .8em;
  line-height:1.3;
}
.dropdown select,
label {
  font-family: AvenirNextCondensed-DemiBold, Corbel, "Lucida Grande","Trebuchet Ms", sans-serif;
}

/* Custom arrow sits on top of the select - could be an image, SVG, icon font, etc. or the arrow could just baked into the bg image on the select */

.dropdown::after {
  content: "";
  position: absolute;
  width: 9px;
  height: 8px;
  top: 50%;
  right: 1em;
  margin-top:-4px;
  z-index: 2;
  background: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 12'%3E%3Cpolygon fill='rgb(102,102,102)' points='8,12 0,0 16,0'/%3E%3C/svg%3E") 0 0 no-repeat;  
  /* These hacks make the select behind the arrow clickable in some browsers */
  pointer-events:none;
}

/* This hides native dropdown button arrow in IE 10/11+ so it will have the custom appearance, IE 9 and earlier get a native select */
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
  .dropdown select::-ms-expand {
    display: none;
  }
  /* Removes the odd blue bg color behind the text in IE 10/11 and sets the text to match the focus style text */
  select:focus::-ms-value {
    background: transparent;
    color: #222;
  }
}

/* Firefox >= 2 -- Older versions of FF (v2 - 6) won't let us hide the native select arrow, so we'll just hide the custom icon and go with native styling */
/* Show only the native arrow */
body:last-child .dropdown::after, x:-moz-any-link {
  display: none;
}
/* reduce padding */
body:last-child .dropdown select, x:-moz-any-link {
  padding-right: .8em;
}

/* Firefox 7+ -- Will let us hide the arrow, but inconsistently (see FF 30 comment below). We've found the simplest way to hide the native styling in FF is to make the select bigger than its container. */
/* The specific FF selector used below successfully overrides the previous rule that turns off the custom icon; other FF hacky selectors we tried, like `*>.dropdown::after`, did not undo the previous rule */

/* Set overflow:hidden on the wrapper to clip the native select's arrow, this clips hte outline too so focus styles are less than ideal in FF */
_::-moz-progress-bar, body:last-child .dropdown {
  overflow: hidden;
}
/* Show only the custom icon */
_::-moz-progress-bar, body:last-child .dropdown:after {
  display: block;
}
_::-moz-progress-bar, body:last-child .dropdown select {
  /* increase padding to make room for menu icon */
  padding-right: 1.9em;
  /* `window` appearance with these text-indent and text-overflow values will hide the arrow FF up to v30 */
  -moz-appearance: window;
  text-indent: 0.01px;
  text-overflow: "";
  /* for FF 30+ on Windows 8, we need to make the select a bit longer to hide the native arrow */
  width: 110%;
}


/* At first we tried the following rule to hide the native select arrow in Firefox 30+ in Windows 8, but we'd rather simplify the CSS and widen the select for all versions of FF since this is a recurring issue in that browser */
/* @supports (-moz-appearance:meterbar) and (background-blend-mode:difference,normal) {
.dropdown select { width:110%; }
}   */


/* Firefox 7+ focus style - This works around the issue that -moz-appearance: window kills the normal select focus. Using semi-opaque because outline doesn't handle rounded corners */
_::-moz-progress-bar, body:last-child .dropdown select:focus {
  outline: 2px solid rgba(180,222,250, .7);
}


/* Opera - Pre-Blink nix the custom arrow, go with a native select button */
x:-o-prefocus, .dropdown::after {
  display:none;
}


/* Hover style */
.dropdown:hover {
  border:1px solid #888;
}

/* Focus style */
select:focus {
  outline:none;
  box-shadow: 0 0 1px 3px rgba(180,222,250, 1);
  background-color:transparent;
  color: #222;
  border:1px solid #aaa;
}


/* Firefox focus has odd artifacts around the text, this kills that */
select:-moz-focusring {
  color: transparent;
  text-shadow: 0 0 0 #000;
}

option {
  font-weight:normal;
}


/* These are just demo button-y styles, style as you like */
.button {
  border: 1px solid #bbb;
  border-radius: .3em;
  box-shadow: 0 1px 0 1px rgba(0,0,0,.04);
  background: #f3f3f3; /* Old browsers */
  background: -moz-linear-gradient(top, #ffffff 0%, #e5e5e5 100%); /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ffffff), color-stop(100%,#e5e5e5)); /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, #ffffff 0%,#e5e5e5 100%); /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, #ffffff 0%,#e5e5e5 100%); /* Opera 11.10+ */
  background: -ms-linear-gradient(top, #ffffff 0%,#e5e5e5 100%); /* IE10+ */
  background: linear-gradient(to bottom, #ffffff 0%,#e5e5e5 100%); /* W3C */
   padding:10px;
}

.output {
  margin: 0 auto;
  padding: 1em; 
}
.colors {
  color: #fff;
  display: none;
}
a {
  color: #c04;
  text-decoration: none;
}

a:hover {
  color: #903;
  text-decoration: underline;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>Tshirt Printing guide made easy.</h2>
We can suggest you a printing method based on the options you choose below:

<div id="question1" class="colors" style="display:block"> 
<h3>It is a logo with solid colours, numbers or letters perhaps?</h3>
<select class="button dropdown" id="select1">
   <option value="--">Please Select</option>
   <option value="question2">Yes</option>
   <option value="question3">No</option>
</select>
</div>
<div id="question2" class="colors" style="display:none"> 
<h3>Do you need more or less than 25 units?</h3>
<select class="button dropdown" id="select2">
   <option value="--">Please Select</option>
   <option value="question4">Less</option>
   <option value="question12">More</option>
</select>
</div>
<div id="question3" class="colors" style="display:none"> 
<h3>Is your design an illustration or a drawing?</h3>
<select class="button dropdown" id="select3">
   <option value="--">Please Select</option>
   <option value="question6">Yes</option>
   <option value="question5">No</option>
</select>
</div>
<div id="question4" class="colors" style="display:none"> 
<h3>Is the size of the design bigger than 10x10cms?</h3>
<select class="button dropdown" id="select4">
   <option value="--">Please Select</option>
   <option value="question8">Yes</option>
   <option value="question9">No</option>
</select>
</div>
<div id="question5" class="colors" style="display:none"> 
<h3>Are you trying to print a photograph?</h3>
<select class="button dropdown" id="select5">
   <option value="--">Please Select</option>
   <option value="question7">Yes</option>
   <option value="question11">No</option>
</select>
</div>
<div id="question6" class="colors" style="display:none"> 
<h3>Does it have solid colours?</h3>
<select class="button dropdown" id="select6">
   <option value="--">Please Select</option>
   <option value="question2">Yes</option>
   <option value="question11">No</option>
</select>
</div>
<div id="question7" class="colors" style="display:none"> 
<h3>Are you after a soft feel print?</h3>
<select class="button dropdown" id="select7">
   <option value="--">Please Select</option>
   <option value="question10">Yes</option>
   <option value="question12">No</option>
</select>
</div>
<div id="question8" class="colors" style="display:none"> 
<h3>Cad Cut Vynil</h3>
</div>
<div id="question9" class="colors" style="display:none"> 
<h3>Embroidery</h3>
</div>
<div id="question10" class="colors" style="display:none"> 
<h3>Direct to Garment (DTG)</h3>
</div>
<div id="question11" class="colors" style="display:none"> 
<h3>Does your design have shadows and gradients?</h3>
<select class="button dropdown" id="select11">
   <option value="--">Please Select</option>
   <option value="question7">Yes</option>
   <option value="question1">No</option>
</select>
</div>
<div id="question12" class="colors" style="display:none"> 
<h3>Screen Printing</h3>
</div>
<div id="question13" class="colors" style="display:none"> 
<h3>Transfer Printing</h3>
</div>
<br clear="all"/>
<input type="button" name="startover" value="Start Over" class="button" onclick="showDiv()" />

Advertisement

Answer

Just playing with this, you can compress it into one loop

document.querySelectorAll('select').forEach((item)=>{
  item.addEventListener('change', (function(){
    document.getElementById(item.value).style.display = 'block';
    $("option:selected").removeAttr("selected");
    document.getElementById('question'+(/[0-9]/gi.exec(item.id)[0])).style.display = 'none';
  }));
});

And it should work when you add more questions. Strange mix of ES6 style and Jquery now (may not work on ie). I would turn away from JQ, it’s not really needed now and probably better to learn JS

function resetQuestions() {
    document.querySelectorAll("[id^='question']").forEach((element)=>{
        element.style.display = "none";
    });
    document.getElementById("question1").style.display = "block"; 
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement