Reputation: 4041
I have some jquery that inputs text into a database and then clears the textarea. However the textarea is not being cleared. Below is the code indicating which line doenst work. When i replace this line with alert(commentbox) I get the value of the comment box so i know that the variable is working. I just don't know how to clear the variable.
Jquery:
<script type='text/javascript'>
$('document').ready(function () {
$('.commentContainer').load('../writecomment.php');
$("form").on("submit", function (e) {
e.preventDefault();
var $form = $(this);
var commentbox = $(this).children('.commentBox').val();
$.ajax({
"url": $form.attr("action"),
"data": $form.serialize(),
"type": $form.attr("method"),
success: function () {
$('.commentContainer').load('../writecomment.php');
commentbox = ""; //this line doesnt work
}
});
});
});
I should also mention that when i replace the bad line with $('.commentBox').val(''); the values clear. the problem is that it clears all the textareas, not just the one that i use .children() to find.
</script>
Upvotes: 1
Views: 220
Reputation: 218732
var $form = $(this);
var commentboxObj = $(this).children('.commentBox');
var commentBoxVal=commentboxObj.val();
$.ajax({
"url": $form.attr("action"),
"data": $form.serialize(),
"type": $form.attr("method"),
success: function () {
$('.commentContainer').load('../writecomment.php');
commentboxObj.val(""); //this will work
}
});
with commentbox=""
, you were trying to update the content of a javascript variable, not the actual textbox.
Upvotes: 0
Reputation: 1409
var commentboxElem = $(this).children('.commentBox');
and in your success:
commentboxElem.val('');
Upvotes: 2