jquery - Javascript code and HTML tag -
jquery - Javascript code and HTML tag -
i'm working on settings page made of form inputs, checkboxes , form submit.
when load page, regardless of previous saved settings, shows nil below <select class="required-entry">
how edit javascript code create work correctly?
here javascript:
$(function() { $('.required-entry').change(function(){ // utilize class or utilize $('select') if ($(this).val() == "3" || $(this).val() == "2") { $("#attach").show(); } else { $("#attach").hide(); } }); }); $(function() { $('.required-entry').change(function(){ // utilize class or utilize $('select') if ($(this).val() == "1" || $(this).val() == "3") { $("#attach2").show(); } else { $("#attach2").hide(); } }); });
here html code:
<select class="required-entry"> <option value="0" selected="true">show nothing</option> <option value="2" selected="true">whatsapp only</option> <option value="1" selected="true">e-mail only</option> <option value="3" selected="true">whatsapp + e-mail</option> </select> <div id="attach" style="display:none;" >whatsapp description</div> <div id="attach2" style="display:none;" >e-mail description</div>
solution
you shouldn't pre-select every alternative , should trigger alter event on page load;
class="snippet-code-js lang-js prettyprint-override">$(function() { $('.required-entry').change(function(){ var val = $(this).val(); if (val == "3" || val == "2") $("#attach").show(); else $("#attach").hide(); if (val == "1" || val == "3") $("#attach2").show(); else $("#attach2").hide(); }); $('.required-entry').trigger('change'); });
class="snippet-code-html lang-html prettyprint-override"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <select class="required-entry"> <option value="0" selected="true">show nothing</option> <option value="2">whatsapp only</option> <option value="1">e-mail only</option> <option value="3">whatsapp + e-mail</option> </select> <div id="attach" style="display:none;" >whatsapp description</div> <div id="attach2" style="display:none;" >e-mail description</div>
javascript jquery forms
Comments
Post a Comment