user829174
user829174

Reputation: 6362

Jquery submit on enter key

I have the following code:

<div class="search">
    <input type="text" id="tbWasSearch" value="Search here..." class="project" onclick="this.value=''"/>
    <input type="button" id="tbWasSeachButton" class="tbWasSeachButton" onclick="searchWas();" />
</div>

    function searchWas() {
        var txt = $("#tbWasSearch").val();
        if (txt != "") {
            var url = "http://eiff/server.pt?=" + txt;
            window.open(url);
            $("#tbWasSearch").val('');
        }
    }

I want that once i put text in tbWasSearch and press enter, the text entered with inoke searchWas(); Should be the same mechanism as when i put text and click the search button

How can this be done?

Upvotes: 0

Views: 462

Answers (4)

Bharat Chodvadiya
Bharat Chodvadiya

Reputation: 1650

You use form something like this..

<script>
function searchWas() {
    var txt = $("#tbWasSearch").val();
    if (txt != "") {
        var url = "http://eiff/server.pt?=" + txt;
        window.open(url);
        $("#tbWasSearch").val('');
    }
}
</script>

<form action="javascript:searchWas();">
<div class="search">
<input type="text" id="tbWasSearch" value="Search here..." class="project" onclick="this.value=''"/>
<input type="button" id="tbWasSeachButton" class="tbWasSeachButton" />
</div>
</form>

Upvotes: 0

webdeveloper
webdeveloper

Reputation: 17288

Use keyuy event:

var reload = function(e) { 
   if (e.keyCode == '13')
      searchWas();
};

$('#tbWasSearch').keyup(reload);

Upvotes: 0

Mahmoud Farahat
Mahmoud Farahat

Reputation: 5485

also , if you use form tag you can use :

$('form:first').submit();

Upvotes: 0

m.piras
m.piras

Reputation: 420

Using jQuery you can write something like this

$("#tbWasSearch").keyup(function(event){
    if(event.keyCode == 13){
        $("#tbWasSeachButton").click();
    }
});

where 13 is the keyCode for the return key

Upvotes: 2

Related Questions