detectar que un input tiene valor en html y javascript in javascript

To detect if an input field has a value in HTML and JavaScript using JavaScript, you can write a function that checks if the value of the input field is not equal to null or an empty string. Here's an example:

index.tsx
function validateInput() {
  var inputField = document.getElementById("myInput");
  var inputValue = inputField.value.trim();

  if (inputValue == null || inputValue === "") {
    // Input field is empty
    return false;
  } else {
    // Input field has a value
    return true;
  }
}
287 chars
13 lines

In this example, we first get a reference to the input field using document.getElementById(). We then use the trim() method to remove any leading or trailing white space from the input value. Finally, we check if the input value is not equal to null or an empty string.

To use this function, you would call it on the onsubmit event of your form, like this:

<form onsubmit="return validateInput()">
  <label for="myInput">Enter a value:</label>
  <input type="text" id="myInput" name="myInput">
  <button type="submit">Submit</button>
</form>
185 chars
6 lines

This code adds a submit button to the form and attaches the validateInput() function to the onsubmit event to validate the input field before submitting the form.

gistlibby LogSnag