Dark Leviathan
Dark Leviathan

Reputation: 489

Delete text in an input box when 'enter' key is pressed

I want to create a google search that when the user presses the 'enter' key, it will search and then delete the text in the input field. My search opens the search in a new tab.

My java code that tries to do that:

final JTextArea ta = new JTextArea();
ta.addKeyListener(new KeyListener() {

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_ENTER){
            ta.setText("");
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {
    }
});
}

My Google Search Input Field Code:

<input class="searchbar1" type="Search" dir="ltr" onload="self.focus();" autofocus required align="center" id="GoogleSearch" value="" name="q" placeholder="" autocomplete="off">

Upvotes: 0

Views: 4991

Answers (3)

markasoftware
markasoftware

Reputation: 12672

Your question seems unclear, but I would recommend adding an onchange handler to the field like this:

document.getElementById('GoogleSearch').addEventListener('change',function(){
    //somehow search for the term here
    //remove text
    document.getElementById('GoogleSearch').value='';
},false);

Note that this actually will also do the search when the user clicks out of the field, now just when they hit enter. If you want exclusively enter button, tell me in the comments

Upvotes: 0

sam
sam

Reputation: 2486

You need to call a jscript function which like this.. call this in your submit button onclick..

function cleartext()
{
document.getElementById(GoogleSearch).text="";
}

Upvotes: 1

Doge
Doge

Reputation: 345

Use javascript's innerHTML to replace the filled input field with the same input field but empty. (<input class="searchbar1" type="Search" dir="ltr" onload="self.focus();" autofocus required align="center" id="GoogleSearch" value="" name="q" placeholder="" autocomplete="off">)

Upvotes: 0

Related Questions