How to prevent the user from making changes in the input?

4

I have input in a form, but it should not have its value changed:

<input type="text" name="pais" value="">

How can I prevent the value from being changed?

<!DOCTYPE html>
<html>
<body>

<form action="/action_page.php">
  Pais: <input type="text" name="pais" value=""><br>
  Estado: <input type="text" name="estado" value=""><br>
  <input type="submit" value="Submit">
</form>

</body>
</html>
    
asked by anonymous 17.11.2017 / 17:31

2 answers

7

Use the readonly attribute of the input element for this:

<input type="text" name="pais" value="" readonly />
    
17.11.2017 / 17:33
2

The other answers already include what was requested, however I would like to leave an alternative using JavaScript:

window.onload = function() {
  document.getElementById('input-disabled').disabled = true;
  document.getElementById('input-readonly').readOnly = true;
}
<input type="text" id="input-disabled">
<input type="text" id="input-readonly">

Through onload , as the page loads its contents, I create a function to get the element with id equal to input-disabled and add the attribute :disabled to true. The same goes for the element with id equal to input-readonly , which adds the attribute :readOnly to true.

As for :disabled and :readonly :

  • Disabled does not pass the value to the form, in addition to being unable to edit.

  • Readonly sends the value to the form and also can not edit.

22.11.2017 / 20:02