Click here to Skip to main content
15,667,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I choose a filter and enter text on the search bar, for example I choose the filter "Location" and search for "London" (see snippet) and then press the button ENTER, the search bar does not search for anything unless I type ?# at the end of the url.

Let's say if the url is: localhost/companyDirectory/ - the search bar created does not search for anything.

Whereas if the url is: localhost/companyDirectory/?# - the search bar created does search.

Could someone have a look and explain what am I doing wrong?

What I have tried:

function search(e) {
  var e = e || window.event;
  var keycode = e.keyCode || e.charCode;
  if (keycode === 13) {


        <p class="findID"></p>

        <div class="hideCell">
            <p class="hideCell" id="departmentHeader"></p>
            <p class="hideCell" id="locationHeader"></p>

            <span class="hideCell"



    var filterBy = $('.filterSelect:first').val()
    var searchText = $('#searchBar').val()


      type: 'GET',
      url: 'php/getAll.php',
      dataType: 'json',

      success: function(data, i) {

        var db =;

        for (let i in db) {

          var searchTextArr = searchText.split(' ')

          for (let idx in searchTextArr) {

            if ((db[i][filterBy].toLowerCase()).indexOf(searchTextArr[idx].toLowerCase()) >= 0) {


                            <div class="loadProfile col-sm-6 col-md-4" onclick="loadProfile(${JSON.stringify(db[i]).split('"').join(""")})">
                                <div class="widget col-3 profile">
                                    <div class="widget-simple">

                                        <img src="img/user-regulars.svg" alt="avatar" class="widget-image img-circle pull-left animation-fadeIn">
                                            <h4 class="widget-content text-left">
                                                <span id="fullName">${db[i].lastName}, ${db[i].firstName}</span>
                                                <p class="findID" style="font-size:11px; color: rgb(190, 190, 190); display: inline"> - ID: ${db[i].id}</p>

                                                <div class="info" style: "overflow: hidden">
                                                    <p class=${filterBy == "department"} style="font-size:11px; color: rgb(190, 190, 190); float: left">${db[i].department}</p>
                                                    <p class=${filterBy == "location"} style="font-size:11px; color: rgb(190, 190, 190); float: left">, ${db[i].location}</p>

                                                    <a href="" class="btn btn-xs btn-primary2 Phone" data-toggle="tooltip" title="" data-original-title="Phone"></a>
                                                    <a href="mailto:${db[i].email}" rel="prefetch" id="eM" class="btn btn-xs btn-primary Email" data-toggle="tooltip" title="" data-original-title="Email"></a>










  return true


<div class="block2">

  <div id="searchData" class="search">

    <table id="tableButton">

      <tbody id="buttonsTop">
        <tr id="buttonTop">
          <td id="buttonFilter">
            <select class="filterSelect">
              <option value="filterBy">Filter by</option>
              <option value="lastName">Last Name</option>
              <option value="firstName">First Name</option>
              <option value="department">Department</option>
              <option value="location">Location</option>



          <td id="buttonSearch">

            <form class="ui-filterable">
              <input class="form-control" id="searchBar" onkeypress="search(event)" type="text" placeholder="Search using the filters">



          <td id="buttonNew" onclick="toggleAddEmployee()"></td>


Updated 17-Feb-21 2:13am

1 solution

It's probably because your input is within a form, and when you press the ENTER button it's submitting the form after your Javascript code is executed. The reason it probably works after you've added # to the end of your URL is because the page doesn't navigate to itself again.

Consider making your search method return false if the ENTER key is pressed, which should suppress the submit. Otherwise, also considering binding directly to the form submit by using
$('form').on('submit', function (e) {
  return false;

This means you don't need to monitor for key-presses and it will ensure that the form is not actually submitted, but just the JS code is run.
Share this answer
farremireia 17-Feb-21 8:54am    
Amazing! Thanks a lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900