0

I have this logic below that is getting all the value using the class name and then doing a looping. In the loop I am checking if any of the values is true. I wanted to see if there is a way to do this without the loop can I just query the class making sure that just one is true then set the ResultValue to true?

                    let File = false;
                    $('.fileClass').each((index, element) => {
                        console.log(element.value);
                        if (element.value == true) {
                            File = "True"
                        }     
                    });
                    if (File === false) {
                        $('#hasFile').val('');
                    }
                    
                     $('#ResultValue').html($('#hasFile').val());
                    
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="hidden" id="hf1" name="hf1" class="fileClass" value="False">
 <input type="hidden" id="hf2" name="hf2" class="fileClass" value="False">
 <input type="hidden" id="hf2" name="hf2" class="fileClass" value="False">
 
 
  <input type="hidden" id="hasFile" name="hasFile" value="True">
  
  
   <label id="ResultValue"></label>
Jefferson
  • 71
  • 3
  • 12

1 Answers1

1

Sure you can, you can querySelector on the CSS class and check if the Value is set to True

const hasFiles = $('.fileClass[value="True"]').length > 0;

if (!hasFiles) $('#hasFile').val('');
    
$('#ResultValue').html($('#hasFile').val());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="hidden" id="hf1" name="hf1" class="fileClass" value="False">
<input type="hidden" id="hf2" name="hf2" class="fileClass" value="False">
<input type="hidden" id="hf3" name="hf3" class="fileClass" value="False">
 
<input type="hidden" id="hasFile" name="hasFile" value="True">
  
<label id="ResultValue"></label>
SamHoque
  • 2,978
  • 2
  • 13
  • 43