1

I want to temporarily disable lname textbox when the fname textbox is empty, but if the fname is not empty the lname should be enabled.

I have a code but I don't think it is correct nor wrong.

const fname = document.querySelector(".fname");
const lname = document.querySelector(".lname");

lname.readonly = true;
fname.addEventListener("keyup", () => {
  if (fname.value.length > 0) {
    lname.readonly = false;
  } else {
    lname.readonly = true;
  }

})
<form action="/action_page.php">
  <label for="fname">Fname:</label>
  <input type="text" id="fname" name="fname"><br><br>
  <label for="lname">Lname:</label>
  <input type="text" id="lname" name="lname" disabled><br><br>
  <input type="submit" value="Submit">
</form>

1
  • Also wrap the fields in the label and give the label display: block and a margin-bottom instead of the <br> saving you the for and the brs Commented Jan 5 at 13:51

5 Answers 5

1

As already said, you need to use the ID selector or getElementById.

Here is a much simpler version

const lname = document.getElementById('lname');
document.getElementById('fname')
  .addEventListener('input', () => lname.disabled = fname.value.trim().length === 0);
label {
  display: block;
  margin-bottom: 5px;
}
<form action="/action_page.php">
  <label>Fname: <input type="text" id="fname" name="fname" /></label>
  <label>Lname: <input type="text" id="lname" name="lname" disabled /></label>
  <input type="submit" value="Submit" />
</form>

Sign up to request clarification or add additional context in comments.

Comments

1

A short solution, using event delegation (note: enables last name field when first name field value length >= 3).

document.addEventListener(`input`, handle);

function handle(evt) {
  if (evt.target.id === `fname`) {
    return document.querySelector(`#lname`).disabled = 
      evt.target.value.length < 3;
  }
}
<!doctype html>
<html>
  <body>
    <form action="/action_page.php">
      <label for="fname">Fname:</label>
      <input type="text" id="fname" name="fname" /><br /><br />
      <label for="lname">Lname:</label>
      <input type="text" id="lname" name="lname" disabled/><br /><br />
      <input type="submit" value="Submit" />
    </form>
  </body>
</html>

2 Comments

Try inserting a text in fname using right mouse click and paste in the context menu -- does it work? There are many ways that you can change the value of an input element.
@chrwahl sure. Fixed.
0

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>

Comments

-1
  1. We should select using #fname since, we are having the ID field as fname, the selector you used is for classes. We can go for the more specific getElementById to fetch the element by ID.

  2. We can use setAttribute and removeAttribute to toggle the disabled condition.

const fname = document.getElementById("fname")
const lname = document.getElementById("lname")
fname.addEventListener("keyup", () => {
  if (fname.value.length > 0) {
    lname.removeAttribute("disabled")
  } else {
    lname.setAttribute("disabled", "true")
  }
})
<!doctype html>
<html>
  <body>
    <form action="/action_page.php">
      <label for="fname">Fname:</label>
      <input type="text" id="fname" name="fname" /><br /><br />
      <label for="lname">Lname:</label>
      <input type="text" id="lname" name="lname" disabled/><br /><br />
      <input type="submit" value="Submit" />
    </form>
  </body>
</html>

1 Comment

Try inserting a text in fname using right mouse click and paste in the context menu -- does it work? There are many ways that you can change the value of an input element.
-1

const fname = document.querySelector("#fname")
const lname = document.querySelector("#lname")
fname.addEventListener("keyup", () => {
  if (fname.value.length > 0) {
    lname.disabled = false;
  } else {
    lname.disabled = "true";
  }
})
<!doctype html>
<html>
  <body>
    <form action="/action_page.php">
      <label for="fname">Fname:</label>
      <input type="text" id="fname" name="fname" /><br /><br />
      <label for="lname">Lname:</label>
      <input type="text" id="lname" name="lname" disabled/><br /><br />
      <input type="submit" value="Submit" />
    </form>
  </body>
</html>

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
Try inserting a text in fname using right mouse click and paste in the context menu -- does it work? There are many ways that you can change the value of an input element.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.