Setting a value to null using jQuery

I have Id City on the page, which is basically a property of the model. I want to set it as null. I am trying to achieve this:

$("#City").val(null); 

But this does not mean that the value is null.

+6
source share
4 answers

You do not set the value of the elements to null , since HTML does not have the concept of null , undefined , etc., you remove the attribute or, even better, set it to an empty string:

 $("#City").val(""); 
+17
source

If you are trying to install / uninstall Id

 $("#City").attr("id",""); 

or use removeAttr ()

If you are trying to set the value inside,

 $("#City").val(""); 
+4
source

Using plain javascript, try using the setAttribute method,

 document.getElementById("City").setAttribute("value" , ""); 

or without using the setAttribute method,

 document.getElementById("City").value = ""; 
+2
source

You can remove the identifier using jQuery prop() or attr() methods, for example:

 $('#City').prop('id', null); 

or like this:

 $('#City').attr('id', null); 

Just in case:

jQuery Documentation for prop () method

jQuery Documentation for attr () Method

0
source

Source: https://habr.com/ru/post/954614/


All Articles