2
I have several arrays that hide or display event-based fields change of a select, the problem is that when iterating the array with the element id only the last value of the array is displayed, as if the instruction within the loop were rewritten and not applied to each item, code:
$('select[name="template"]').on('change', function() {
var template_value = $(this).val();
switch(template_value) {
case 'template_1':
var visible_fields_template = ['model', 'capacity', 'color', 'hour', 'link'];
break;
case 'template_2':
var visible_fields_template = ['model', 'capacity', 'name', 'color', 'hour', 'link'];
break;
}
$.each(visible_fields_template, function(index, value) {
$('div.teste_fields:not([id="'+value+'"])').hide();
$('div.teste_fields[id="'+value+'"]').show();
});
$('div.template:not([id="'+template+'"])').hide();
$('div.template[id="'+template+'"]').show();
});
HTML:
<form class="col-md-10">
<fieldset>
<div class="row">
<div class="col-md-12 mb-20" id="template">
<label for="template">Template *</label>
<select class="form-control " name="template">
<option value="template_1">Modelo 1</option>
<option value="template_2">Modelo 2</option>
</select>
</div>
</div>
<div class="row teste_fields" id="model">
<div class="col-md-12 mb-20">
<label for="model">Modelo *</label>
<select class="form-control " name="model">
<option value="celular_1">celular 1</option>
<option value="celular_2">celular 2</option>
</select>
</div>
</div>
<div class="row teste_fields" id="capacity">
<div class="col-md-12 mb-20">
<label for="capacity">Capacidade *</label>
<select class="form-control " name="capacity">
<option value="8GB">8GB</option>
<option value="16GB">16GB</option>
</select>
</div>
</div>
<div class="row teste_fields" id="color">
<div class="col-md-12 mb-20">
<label for="color">Cor *</label>
<select class="form-control " name="color">
<option value="Silver">Silver</option>
<option value="Space Gray">Space Gray</option>
</select>
</div>
</div>
<div class="row teste_fields" id="link">
<div class="col-md-12 mb-20">
<label for="link">Link *</label>
<input name="link" placeholder="http:www.site.com" class="form-control" type="text">
</div>
</div><!--row1-->
<div class="row teste_fields" id="hour">
<div class="col-md-12 mb-20">
<label for="nome">Hora *</label>
<input name="hour" class="form-control" type="text">
</div>
</div><!--row1-->
</fieldset>
</form>
in that case only the #link would be visible, how can I fix this?
this is inside the event change(), I did not put because I thought it would not be necessary
– Thiago
in fact all the logic is already done, the problem is that inside the loop it only hides/displays the last element of the array
– Thiago
But this is clear. The loop is hiding all but the time item in the array, and that in each, when it reaches the end, only the last will be visible.
– Sam
1st iteration: all invisible except "model".... 2nd iteration, all invisible except "Capacity"... and so on
– Sam
Add to the question what you really want. It’s not very clear. In the question you say "how can I fix this?" but it doesn’t say what would be right.
– Sam
shows your html code
– 13dev
truth, dvd, this part was a little confused, I will update the code, but basically what I would like to do is to make visible only the elements that are in the array.
– Thiago
@NGTHM4R3 I updated the answer. I hope I can help.
– Sam