elmage
elmage

Reputation: 19

how do i trigger search with the enter key

i need to trigger search with the enter key but its not working, this is the code i used. any help will be appreciated.

 $('#header input[name=\'search\']').bind('click', function(e) {

    if (e.keyCode == 13) {

        url = $('base').attr('href') + 'index.php?route=product/search';

        var search = $('input[name=\'search\']').attr('value');

        if (search) {
            url += '&search=' + encodeURIComponent(search);
        }

        location = url;
    }
});

Upvotes: 1

Views: 817

Answers (2)

john Smith
john Smith

Reputation: 17936

you most likely want to catch the keyup event and not click

 $('#header input[name=\'search\']').bind('keyup', function(e) {

    if (e.keyCode == 13) {
        console.log("FOOOOOO");
        url = $('base').attr('href') + 'index.php?route=product/search';

        var search = $('input[name=\'search\']').attr('value');

        if (search) {
            url += '&search=' + encodeURIComponent(search);
        }
        console.log("url: "+url);
        window.location.href  = url;
    }
});

i dont know what youre try to achive but i would code this selector :

$('#header input[name="search"]')...

updated

// 
$('input[name="search"]').bind("enterKey",function(e){
   console.log("yeah i just triggered");
});


$('input[name="search"]').keyup(function(e){
    if(e.keyCode == 13)
    {
        $(this).trigger("enterKey");
    }
});

Upvotes: 3

Rusher
Rusher

Reputation: 507

If it's possible, you'd better use

<input type="submit" value="">

wrap inputs into a <form>

and stylize button

input[type='submit'] {height: 0; width: 0; overflow: hidden;}

If you need to change action url in javascript, use something like

$('form').submit ( function() {
    $(this).attr('action','/route-to-submit');
})

Upvotes: 0

Related Questions