Category Archives: Ajax

Ajax

[RESOLVED]How to reset a textarea to nothing

Hi how do I empty a textarea. 

I tried:

$(‘#comments’).val();

$(‘#comments’).text();

But none work. If you add a string you can set the value but how do you make it nothing?

You need an empty string in the method

.val(”)

You have to pass a value in to val for it to work. Do this:

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

$("#comments").val(”);

I tried that too, no effect. Any ideas?

I tried that too, but does not work.

Not sure what the problem could be.

Are you sure your code is being called/hit? Are you sure you’re using the right selector? Maybe use some alert() to debug a bit. 

Yes I put an alert after it.

Its weird it will allow me to set the text though.

damon2012

Yes I put an alert after it.

Its weird it will allow me to set the text though.

If it is server control you might want to use ClientID as shown below:

$('#<%=comments.ClientID%>').val('');

Also make sure that control ID is correct.
 

<!DOCTYPE html>
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<form name = "myform">
 <textarea id="comments"></textarea>
<input type="button"  value="Test1"
      onclick="$('#comments').val('ddd');">
  <input type="button"  value="Test2"
      onclick="$('#comments').val('');">
</form>
</body>
</html>

Tested at http://jsbin.com/inahiw/1/edit

Thanks . Got it working it turns out that I was loading a parital view and therefore it was not hitting it the second time around.