简体   繁体   中英

check if an element has length zero in an array and do something with that element on javascript

I want to know if some elements in an array has length zero, and with those elements I want to change the color for example. all of this with javascript es5.

for example, I have a form with some inputs in there, I want to check if there is any input in that form with value length equal zero, and for those inputs that has value length equal zero I want to change the border color to red.

The following code snippet may help you in achieving the results.

 const checkVal = function(){ let inputs = document.getElementsByTagName("input"); for(input of inputs){ if(input.value.length == 0) input.style.borderColor="red"; else input.style.borderColor="black"; } }
 <input type="text"> <input type="text"> <input type="text"> <a href="javascript:" onclick="checkVal()">Check Empty Inputs</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM