1

How can I set automatically clear my html input after click enter? It's complicated because my program uses ajax and php also. I tried to do this by jQuery $("#txt").val('') but then my input is clearing every single typed letter (I need to clear input only after click enter).

<html>
<body>
<div><label for="txt">Text:
<input id="txt" name="txt" type="text" value="" size="20"></label></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js">
</script>

<script>
   $(window).load(function() {
       $("#txt").keyup(function(event) {
           if (event.keyCode == 13) 
            $.ajax({
                url: 'hello.php',
                data: {
                    func: 'test',
                    txtVal: $("#txt").val()
                },
                type: 'post',                                   
                });
              $("#txt").val('')
           });
    });
</script>

1 Answer 1

1

There are problem with your if statement. Try to fix it with this code:

<html>
<body>
<div><label for="txt">Text:
    <input id="txt" name="txt" type="text" value="" size="20"></label></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js">
</script>

<script>
    $(window).load(function() {
        $("#txt").keyup(function(event) {
            if (event.keyCode == 13) {
                $.ajax({
                    url: 'hello.php',
                    data: {
                        func: 'test',
                        txtVal: $("#txt").val()
                    },
                    type: 'post',
                });
                $("#txt").val('')
            }
        });
    });
</script>  
Sign up to request clarification or add additional context in comments.

1 Comment

Try to change this to type: "POST" as in jquery manual api.jquery.com/jquery.post

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.