window.location.href does not work for Enter key event in search box

Jason

I'd like to implement a search box on my site. To be more specific, I want to invoke the search function when I hit enter key in the search box. The html code block I use:

<li class="search-box">
<form class="navbar-form navbar-left" role="search">
    <div class="form-group">
        <input type="text" id="search-box" class="form-control" placeholder="Search...">
    </div>
    <button type="button" class="btn btn-default search-submit">Submit</button>
</form>

And the js code block:

function search() {
    let query = $('input').val().trim();
    console.log('query: ' + query);
    window.location.href = '?query=' + query;
}

// this works.
$('.search-submit').on('click', function () {
    search();
});

// this does not work ):
$('#search-box').keydown(function (e) {
    if (e.keyCode === 13) {
        search();
    }
});

The query has been logged when I hit enter key, but the redirection does not succeed. Any hints?

Jay Lim

Pressing enter attempts to submit the html form. That's the default behavior. Thus it will redirect to the same page since your form element does not contain the action attribute. You can disable the form's default behavior by calling e.preventDefault().

Here's what you can do:

$('#search-box').keydown(function (e) {
  if (e.keyCode === 13) {
    e.preventDefault();
    search();
  }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why href="javascript:window.location.href='#xxx';" does't work?

From Dev

onchange event on window.location.href

From Dev

Javascript window.location does not work

From Dev

window.location.href doesn't work with url scheme

From Dev

JavaFX WebView's edit box accepts ENTER key event

From Dev

typeahead.js search box navigation with enter key

From Dev

Why does setting window.location.href not stop script execution

From Dev

What does loc = $('<a>', {href:window.location})[0]; mean?

From Dev

Issue in handling Key ENTER event in search interface of an android fragment

From Dev

Why window onscroll event does not work?

From Dev

jqGrid InlineEdit "ENTER" key does not trigger "aftersavefunc" event?

From Dev

window.location.href and location.reload() does not load updated page from Server

From Dev

String is not a function on window location href

From Dev

window.location.href not working

From Dev

Why is event.state on window.onpopstate empty when pushing with same location.href (or empty)

From Dev

$.post() doesn't work if there's a re-direct after it: window.location.href =

From Dev

javascript window.location.href doesn't work with IDN-hebrew domain

From Dev

Baidu maps on Android: access key does not work for location searching

From Dev

how do i trigger a button with the enter key in java script?/ how to give text box and search button an ID?

From Dev

why does browser behavior differ when using two different window.location.href assignments?

From Dev

If window.location.href does not match a certain string, add css class to element

From Dev

window.scroll does not work for "home" key and "#" link?

From Dev

Enable and disable window.location.href

From Dev

onclick window.location.href with input value

From Dev

Javascript window.location.href onClick not working

From Dev

Passing parameter through "window.location.href"

From Dev

Window.location.href infinite loop

From Dev

Stubbing window.location.href with Sinon

From Dev

window.location.href not working in IE 11

Related Related

  1. 1

    Why href="javascript:window.location.href='#xxx';" does't work?

  2. 2

    onchange event on window.location.href

  3. 3

    Javascript window.location does not work

  4. 4

    window.location.href doesn't work with url scheme

  5. 5

    JavaFX WebView's edit box accepts ENTER key event

  6. 6

    typeahead.js search box navigation with enter key

  7. 7

    Why does setting window.location.href not stop script execution

  8. 8

    What does loc = $('<a>', {href:window.location})[0]; mean?

  9. 9

    Issue in handling Key ENTER event in search interface of an android fragment

  10. 10

    Why window onscroll event does not work?

  11. 11

    jqGrid InlineEdit "ENTER" key does not trigger "aftersavefunc" event?

  12. 12

    window.location.href and location.reload() does not load updated page from Server

  13. 13

    String is not a function on window location href

  14. 14

    window.location.href not working

  15. 15

    Why is event.state on window.onpopstate empty when pushing with same location.href (or empty)

  16. 16

    $.post() doesn't work if there's a re-direct after it: window.location.href =

  17. 17

    javascript window.location.href doesn't work with IDN-hebrew domain

  18. 18

    Baidu maps on Android: access key does not work for location searching

  19. 19

    how do i trigger a button with the enter key in java script?/ how to give text box and search button an ID?

  20. 20

    why does browser behavior differ when using two different window.location.href assignments?

  21. 21

    If window.location.href does not match a certain string, add css class to element

  22. 22

    window.scroll does not work for "home" key and "#" link?

  23. 23

    Enable and disable window.location.href

  24. 24

    onclick window.location.href with input value

  25. 25

    Javascript window.location.href onClick not working

  26. 26

    Passing parameter through "window.location.href"

  27. 27

    Window.location.href infinite loop

  28. 28

    Stubbing window.location.href with Sinon

  29. 29

    window.location.href not working in IE 11

HotTag

Archive