Get rid of all the global variables, and then use the input event instead of the keyup event. The input event will fire each time the value of the input element changes, no matter how the text was entered (using keyboard, mouse context menu, drag'n'drop etc.).
If you set the required property on fname you can test if the value is somehow valid. In the example I use e.target.validity.valueMissing to decide if the lname should be disabled. If you set the minlength attribute on the input element you can also use e.target.validity.tooShort to test if it is valid.
And also, try not to use IDs, they need to be unique throughout the document. Use the name attribute on the form and input elements and refer to the elements using the dot-notation.
document.forms.form01.fname.addEventListener('input', e => {
e.target.form.lname.disabled = e.target.validity.valueMissing;
});
form {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 5px;
}
<form name="form01" action="/action_page.php">
<label>Fname: <input type="text" name="fname" minlength="3" required></label>
<label>Lname: <input type="text"name="lname" disabled></label>
<button type="submit">Submit</button>
</form>
Usability-wise this disabling of the lname input is maybe not a good idea. An alternative could be to have both input elements required. Basically you don't care about in what order the user types first and last name. This could be an alternative:
form {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 5px;
}
<form name="form01" action="/action_page.php">
<label>Fname: <input type="text" name="fname" required></label>
<label>Lname: <input type="text"name="lname" required></label>
<button type="submit">Submit</button>
</form>
<br>saving you theforand the brs