I have an issue on this codepen https://codepen.io/Kimple/pen/MWEBGZy?editors=0011
In the function titleCheck(), I want to get the value of <input type="text" id="title" placeholder="title">
But I always get an empty value, and I don't know why.
I tried the same code on JSFIDDLE and had the same issue.
You saved the value into the variable when initialized, therefore when the input is empty. The value does not update itself automatically. You need to ask for it with every key press. This way it should log the correct value:
function titleCheck() {
title.addEventListener('keyup', (event) => {
console.log(title.value) // ask for the value every time to see it updating
titleValue = title.value
if (titleValue != '') {
checkBox.checked = true;
checkboxCheck();
} else {
checkBox.checked = false;
checkboxCheck();
}
})
}
titleCheck()
Also next time try using snippets in your answer instead of link to codepen or other external editor.
You have to reassign titleValue, because you set it to empty on beginning and don't change it afterwards
function titleCheck() {
title.addEventListener('keyup', (event) => {
titleValue = document.getElementById("title").value
console.log(titleValue)
if (titleValue != '') {
checkBox.checked = true;
checkboxCheck();
} else {
checkBox.checked = false;
checkboxCheck();
}
})
}