March 22, 201610 yr Trying to get this script to do this: if any of the td text inputs are empty in this tr do this else so something else. My Table <table id="project"> <tbody> <tr> <td><input type="button" value="×" class="delete"></td> <td><input type="button" value="∧" class="move up" ></td> <td><input type="button" value="∨" class="move down"></td> <td><input name="data[0][0]" data-field="description" placeholder="Description" required size="40"></td> <td><input name="data[0][1]" data-field="quantity" placeholder="Q" size="1" required></td> <td><input name="data[0][2]" data-field="price" placeholder="P" size="7" required></td> <td><input name="data[0][3]" data-field="total" placeholder="T" size="7" readonly required></td> <td><input type="button" value="+" class="add"></td> </tr> </tbody> </table> My JS if ($('input:text').val().length == 0) { alert("empty") }else{ alert("Not empty") } This kind of works, if I use this it will display "empty" if they are all empty, but if i input text into just one of them then it goes to the else. Anyone know how this could work?
March 22, 201610 yr $.val() when applied to a collection will only return the value of the first element. To check all the values you'll have to iterate through each input or filter them. if ($('input:text').filter(function() { return this.value.trim() === ""; }).length > 0) { alert("empty") } else { alert("Not empty") }
Create an account or sign in to comment