Got a jQuery loop that checks all the classes testfieldvalue for what the attribute value they have, then shows a number of input fields that is matching the value.
The jQuery loop picks up both values 1,2 but it displays 2 input fields at the first thefieldvalue class, not one at the first thefieldvalue and two at the second thefieldvalue as it should. What am i doing wrong?
Please look at working example: https://jsfiddle.net/m9xr870f/
var testfieldvalue = document.getElementsByClassName("testfieldvalue")[0].value;
var testfieldshow = document.getElementsByClassName("testfieldshow")[0];
var field1 = document.getElementsByClassName("Field_1")[0];
var field1label = document.getElementsByClassName("Field_1_label")[0];
var field2 = document.getElementsByClassName("Field_2")[0];
var field2label = document.getElementsByClassName("Field_2_label")[0];
$(".testfieldvalue[value]").each(function(){
var testfield = ($(this).attr('value'));
if (testfield == '1') {
testfieldshow.style.display = '';
field1.style.display = '';
field1label.style.display = '';
} else if (testfield == '2') {
testfieldshow.style.display = '';
field1.style.display = '';
field1label.style.display = '';
field2.style.display = '';
field2label.style.display = '';
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Input 1
<input type="hidden" value="1" class="testfieldvalue">
<div class="testfieldshow" name="testfieldshow" style="display: none">
<label class="Field_1_label" style="display: none">Field 1</label>
<input type="text" class="Field_1" name ="Field_1" style="display: none">
<label class="Field_2_label" style="display: none">Field 2</label>
<input type="text" class="Field_2" name ="Field_2" style="display: none">
</div>
<br>
Input 2
<input type="hidden" value="2" class="testfieldvalue">
<div class="testfieldshow" name="testfieldshow" style="display: none">
<label class="Field_1_label" style="display: none">Field 1</label>
<input type="text" class="Field_1" name ="Field_1" style="display: none">
<label class="Field_2_label" style="display: none">Field 2</label>
<input type="text" class="Field_2" name ="Field_2" style="display: none">
</div>