Michael Harris
Michael Harris

Reputation: 79

Clear text field on pressing enter

(Solved. Sorry for wasting your guys' time.)

In a web-based app I'm developing for personal use (online community), users have the option of using a "command line" at the top, versus navigating through numerous pages. This saves time, sanity, and executes the code once.

The command line works. As of right now, when you hit enter (there is no submit button), it opens a new tab, runs "cmdline.php" with the relevant $_POST information, and then closes that tab when it's finished executing.

The problem comes in when the tab closes, the old text is still there, "NEW;1283;JSMITH". To avoid people having to highlight and delete it for another entry, I'm trying to figure out how to have it self-clear when you hit enter.

Here is what I have found so far. Note that I'm brand new to javascript/jquery.

    <script src="java/jquery.js"></script>
    <script type="text/javascript">
    $('#cmdline').bind('keyup', function(e) {
        if (e.keyCode === 13) {
        $('#cmdline').find('input:text').val(''); 
        }
    });
    </script>

(later in the same file)

    <form   target="_blank"
            action="cmdline.php"
            method="POST"
            id="cmdline"
    >
        <input type="text" placeholder="Ready for Command" autocomplete="off" name="cmdline">
    </form>

Upvotes: 2

Views: 1318

Answers (2)

Michael Harris
Michael Harris

Reputation: 79

Just fixed it. I knew as soon as I posted a question I would figure it out...

    <script src="java/jquery.js"></script>
    <script type="text/javascript">

    function emptyText() {
        $('#cmdline').find('input:text').val('')
    }

    </script>

    <form   target="_blank"
            action="cmdline.php"
            method="POST"
            id="cmdline"
            onsubmit="emptyText()"
    >
        <input type="text" placeholder="Ready for Command" autocomplete="off" name="cmdline">
    </form>

Upvotes: 1

brk
brk

Reputation: 50346

If you want to use attribute-selector change 'input:text' to this 'input[type="text"]'

$('#cmdline').bind('keyup', function(e) {
  if (e.keyCode === 13) {
    $('#cmdline').find('input[type="text"]').val('');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form target="_blank" action="cmdline.php" method="POST" id="cmdline">
  <input type="text" placeholder="Ready for Command" autocomplete="off" name="cmdline">
</form>

Upvotes: 0

Related Questions