Search code examples
javascriptjqueryecmascript-5

Knowing if all input boxes are empty in ES5 (what is the ES5 equivalent of this ES6?)


Trying to know if all input boxes with an ID starting with 'txtYear_' are empty. I have the following ES6 code, but I would like a ES5 equivalent:

Thanks!

let allNotEmpty = Array.from($("[id^='txtYear_']")).every(function(e) {
  return e.value !== "";    
})

console.log(allNotEmpty);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="txtYear_1" value="1">
<input id="txtYear_2" value="2">
<input id="txtYear_3" value="3">
<input id="txtYear_4" value="4">
<input id="txtYear_5" value="5">
<input id="txtYear_6" value="6">
<input id="txtYear_7" value="7">


Solution

  • You could use the .each function from jQuery.

    var allNotEmpty = true;
    
    $("[id^='txtYear_']").each(function(i, el) {
        allNotEmpty = allNotEmpty && el.value !== "";
    });
    
    console.log(allNotEmpty);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input id="txtYear_1" value="1">
    <input id="txtYear_2" value="2">
    <input id="txtYear_3" value="3">
    <input id="txtYear_4" value="4">
    <input id="txtYear_5" value="5">
    <input id="txtYear_6" value="6">
    <input id="txtYear_7" value="7">