Reputation:
I have a problem with my js code. I have managed to disable the submit button, if there is no input value, but now I want to enable it again when a value is present. Thank you very much
// submit message
submitButton.addEventListener("click", (e) =>{
if(userMessage.value = " ") {
submitButton.setAttribute("disabled", true);
alert("fill field");
} if(userMessage.value != " ") {
submitButton.setAttribute("disabled", false);
alert("send");
}
// prevent default
e.preventDefault();
})
Upvotes: 0
Views: 2983
Reputation: 116
Simple and short approach:
<button>
by defaultoninput
event to <input/>
which toggles the disabled status of the<button>
try
<input type="text" id="userMessage" value="" oninput="this.value == '' ? document.querySelector('#submitButton').disabled = true : document.querySelector('#submitButton').disabled = false"/>
<button disabled id="submitButton">Submit</button>
Upvotes: 0
Reputation: 7585
There are several problems with your code:
" "
with =
to the userMessage
, but you probably intended to check if the value is empty or not. Either use ==
or even better ===
." " !== ""
). Use .trim()
to actually trim all whitespace around a value.else
statement.input
event of your text field here and enable/disable the button.disabled
to false
with setAttribute
won't work here as setAttribute
converts it into a String. So technically it is still trueish. Instead directly set disabled
on your input element, or use removeAttribute
to get it enabled again.Here is an example, using the input
event for disabling and enabling the button. Note, that the button is not initially disabled. To fix that, I created the checkIfSet
function, so I can just set it on the addEventListener
and also just call it to check if the button needs to be disabled or not.
const submitButton = document.querySelector('#submitButton');
const userMessage = document.querySelector('#userMessage');
const checkIfSet = () => {
const trimmedInput = userMessage.value.trim();
if (trimmedInput == '') {
submitButton.disabled = true;
console.log('disabled');
} else {
submitButton.disabled = false;
console.log('enabled');
}
};
const submit = (e) => {
console.log('send');
e.preventDefault();
}
// submit message
userMessage.addEventListener('input', checkIfSet);
submitButton.addEventListener('click', submit);
checkIfSet();
button:disabled {
background: red;
opacity: .5;
}
<input type="text" id="userMessage" />
<button id="submitButton">button</button>
Upvotes: 0
Reputation: 1
add event on input
//when page loads to be disabled
submitButton.setAttribute("disabled", true);
//check when something is changed
userMessage.addEventListener("keyup", (e) =>{
submitButton.setAttribute("disabled", this.value.trim() == "");
})
Upvotes: 0
Reputation: 15
Maby i dont understand it right but you want to click on a disabled button? To enable it? I dont think that works maby try it when the user types in the input field. So
// submit message
InputField.addEventListener("keyup", (e) =>{
if(e.target.value = "") {
submitButton.setAttribute("disabled", true);
alert("fill field");
} else if(e.target.value != "") {
submitButton.setAttribute("disabled", false);
alert("send");
}
})
something like this
Upvotes: 0
Reputation: 2201
You assign a variable instead of condition and second one you are giving single space.
submitButton.addEventListener("click", (e) =>{
if(userMessage.value == "") {
submitButton.setAttribute("disabled", true);
alert("fill field");
} if(userMessage.value != "") {
submitButton.setAttribute("disabled", false);
alert("send");
}
// prevent default
e.preventDefault();
})
Upvotes: 1
Reputation: 398
You could add an event listener to the input element and listen for the keyup event. like:
inputField.addEventListener('keyup', e => {
submitButton.setAttribute("disabled", e.target.value === "");
});
Upvotes: 0