How to clear the content of the html input text field using Jquery

Yasitha picture Yasitha · Aug 13, 2011 · Viewed 18.9k times · Source

I am having a html text field which can hide.

When hiding I want clear the text field.

I used this and it doesn't work.

$('#id').val("");

And I tried $('#id').text(""); as well.

It removed the whole text field.

Answer

John Green picture John Green · Aug 13, 2011

This is correct:

$('#id').val('');

However, your part 2 is interesting.

$('#id').text("");

That shouldn't create the behavior you're describing. I expect that you're referencing the parent object, not the <input> directly.