I have an Id City on page which is basically a model property. I want to set it as null. I am trying this to achieve it:
$("#City").val(null);
But it is not setting the value to null.
Set the value NULL of input field using document. getElementById('myInput'). value = ”
To solve the "Cannot set property of null" error, make sure that the DOM element you are accessing exists. The error is often thrown when trying to set a property after using the getElementById() method and passing it a non-existent id. Copied! const el = document.
You can't tell a text input to have any kind of null value. Empty checkboxes have a value of an empty string '' by definition. The best way to do it, as you say, is to have a checkbox that toggles the disabled property of the text input, which gives a similar semantic.
You don't set an elements value to null
, as HTML has no concept of null
, undefined
etc, you remove the attribute, or better yet, set it to an empty string:
$("#City").val("");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With