Clear Text field after displaying message

Hey There,

I am trying to clear the message text field after displaying message.

Here’s code snippts.

 $("#chatText").keyup(function (e) {
        if (e.keyCode === 13) {
            $chatText = $("#chatText").val();
            $.post('../insertchat.php', {'chatText': $chatText}, function (data, status) {
                $("#chatText").value("");
                if (status === "success") {
                    $("#chatmessage").load("../displayMessage.php", function (data, status) {

                    }, 3000);
               //here i am  trying to clear the fileds but didn't worked for me.
                    $(body).find('input, textarea').val(" ");
                    $("#chatText").value(" ");
                } else {
                    // alert("fuuuuuuuuuuuu");
                }
            });
        }
    });

There’s no PHP in this code as far as I can see, you might get more response if you post in the JavaScript section.

If you check the console, you’ll note that you get a reference error because body is not defined – it shoould be $(document.body).find('input, textarea'), or actually just $('input, textarea').

Not worked :worried:

Works for me (fiddle)… so that probably means the code never runs. Is the request actually successful? Are there any other errors in the console?

 if (status === "success") {
                    $("#chatmessage").load("../displayMessage.php", function (data, status) {

                    }, 3000);

They both run sucessfully.

But this is not working after that

  $(body).find('input, textarea').val(" ");
                    $("#chatText").value(" ");

Any suggestion?

Ah yes, there’s another error in that 2nd line. Just check the console. ;-)

Thanks for pointing out.

Actually I was using

$("#chatText").value(" ");

But it should be

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

:frowning:

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.