obtener todos los elementos de tipo input text y comprobar de que no tengan valores vacios in javascript

To obtain all the input text elements in a web page and validate that they do not have empty values using JavaScript, we can use the querySelectorAll method to select all the input elements of type text and loop through them to check if they have any value or not.

index.tsx
const inputs = document.querySelectorAll('input[type="text"]');

let allInputsFilled = true;

inputs.forEach(input => {
  if(input.value.trim() === '') {
    allInputsFilled = false;
  }
});

if(allInputsFilled) {
  console.log('All inputs are filled');
} else {
  console.log('Not all inputs are filled');
}
309 chars
16 lines

In the code above, we first use querySelectorAll method to get all the input elements of type text. We then set a flag variable allInputsFilled to true, assuming that all inputs have a value. We loop through each input element and check if its value is empty, if it is empty, we set the flag allInputsFilled to false. Finally, we check the value of allInputsFilled to determine if all inputs have a value or not.

gistlibby LogSnag