Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting value to null using jQuery

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.

like image 429
Nitish Avatar asked Sep 25 '13 15:09

Nitish


People also ask

How do you make an input null?

Set the value NULL of input field using document. getElementById('myInput'). value = ”

Can not set value to null?

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.

How do you input null in HTML?

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.


1 Answers

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(""); 
like image 132
adeneo Avatar answered Sep 16 '22 14:09

adeneo