If input doesn't have a value function doesnt triggger

Aidan Young

I am creating a website and I'm wondering how I create something where when a user enters an input and no value is submitted, instead of a function called togglePopup() occurring a function called togglePopup2() will occur. How can I accomplish this? Here is my current code:

<form  autocomplete="off" >
            <input style="border: 2px solid #222; border-radius: 6px;" type="text" id="lname" name="lname" maxlength="10"><br><br><br>
            <input style="border: none; border-radius: 6px; color: ivory; text-shadow: 0px 10px 20px; #222;background: dodgerblue; padding: 15px; font-size: 15px;" value="Submit environment name" type="button"  onclick="togglePopup()">
          </form><script>
              var x;
document.querySelectorAll('input')[1].addEventListener("click", ()=>{
  x = document.querySelector('input').value;
})
          </script> 

Currently a function called togglePopup() occurs when the button is clicked submitting the input. How can I make it so the function only occurs when there is text in the input and if nothing is submitted, an alternate function will occur called togglePopup2() will occur. How can this be done?

Jay Hewitt

const togglePopup = ()=>{
  document.querySelector('button').style.backgroundColor = "green";
}
const togglePopup2 = ()=>{
  document.querySelector('button').style.backgroundColor = "red";
}
document.querySelector('form').onsubmit = (e)=>{
  e.preventDefault();
  console.log(document.querySelector('input').value.trim() === "");
  if(document.querySelector('input').value.trim() === ""){
    togglePopup2();
  } else {
    togglePopup();
  }
};
input{
  border: 2px solid #222;
  border-radius: 6px;
  margin: 5px 0;
}

button{
  border: none;
  border-radius: 6px;
  color: ivory;
  text-shadow: 0px 10px 20px;
  background: dodgerblue;
  padding: 15px;
  font-size: 15px;
  margin: 5px 0;
}
<form  autocomplete="off" action="#" method="post">
  <div>
    <input type="text" id="lname" name="lname" maxlength="10">
  </div>
  <div>
    <button type="submit">Submit environment name</button>
  </div>
  <div id="result"></div>
</form>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

General error Telecom doesn't have a default value with function in model

PowerShell: Function doesn't have proper return value

Input value doesnt update

Python function - if input variable doesn't exist then use default value

Input doesn't add a value to a FORM when it is added by .html function

Function doesnt return a value

input value doesnt be updated with jquery

Splitting an input that doesn't have blank spaces

Potential False Positive warning in VS? "DF0108: Activity function named '{myfunction}' doesn't have an input"

Set state the value of variable in a function that returns function which doesn't have stateful widget

Moq function call always returning value even though input doesn't match mocked input values

Prevent a form from sending if one of its input doesn't have a certain value

React ref using reactstrap Input Module doesn't have Value prop

Progress<T> doesn't have Report function

nodejs function doesnt return a value

swift function doesnt return a value

Function doesn't change value

Should a Python function that doesn't return a value have `-> None` in its signature?

Function passed and invoked in child component doesn't have access to parent state value

Why doesn't Python have a sign function?

Async function in React doesn't have access to this

Node readline module doesn't have 'on' function?

Python 3 doesn't have the file function

Why doesn't Python have an instancemethod function?

<select> doesn't have a selected value

Doesn't have a default value error ! Laravel

Field doesn't have a default value

Horizontal axis doesn't seem to have a value

Why doesn't automatic move work with function which return the value from rvalue reference input?