21

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.

1
  • 3
    By "it" do you mean the value of the element, the innerHTML of the element, or the ID of the element? Commented Sep 25, 2013 at 15:45

4 Answers 4

43

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(""); 
Sign up to request clarification or add additional context in comments.

Comments

6

If you are trying to set/remove Id

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

or use removeAttr()

If you are trying to set the value inside that ,

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

Comments

3

Using normal javascript , try using setAttribute method ,

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

or without using the setAttribute method,

document.getElementById("City").value = "";

Comments

0

You can remove Id using jQuery prop() or attr() methods like this :

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

or like this :

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

Just in case :

jQuery Documentation for prop() method

jQuery Documentation for attr() method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.