JavaScript I hit the ENTER key on keyboard and page refreshes. How do I prevent this and still use the keyboard to search?

We used to be able to enter a search field, address on search box, hit the ENTER key on the keyboard and get the search results.

I made several changes but can't pinpoint the change that resulted in the ENTER key misbehaving. Instead of submitting, it refreshes the page.

I have tried each of the following to stop the page refresh:

<form onSubmit="return false;">

<form onkeypress="return event.keyCode != 13">

Each works.

However, I can no longer hit the ENTER key and have results displayed.

What do I need to do to fix this?

Below is the js:

function getData()
{

     dijit.byId("advanceSearchDialog").hide(); 
    var form = document.getElementById("searchForm");
    var form2 = document.getElementById("featuresForm")
    var searchText = form.searchBox.value.replace(/-/g,"");
    form.searchBox.value = searchText;

    if (searchText != "") 
    {
        // collect features to search for:
        var features = [ ];
        var featTypes = form2.featType;
        for ( var f = 0; f < featTypes.length; ++f )
        {
            if ( featTypes[f].checked ) features.push( featTypes[f].value );
        }
        featureList = "'" + features.join("','") + "'";

        searchMsg("Searching for '" + searchText + "' ...");
        featureID = "";
        var accord = dijit.byId("accordianContainer");
        var resultsPane = dijit.byId("resultsPane");
        accord.selectChild(resultsPane,true);
        doGlobalSearch( searchText, featureList );
    }
    else
    {
      searchMsg("No search criteria entered, enter search text");
    }   
}


function searchKey(e){
    // special case for IE to capture <enter> in the search box
    var key = window.event ? e.keyCode : e.which;
    var keychar = String.fromCharCode(key);
    if (key == 13)
      getData();
}



<form id="searchForm" class="search_field" method="get" action="">
   <input name="searchBox" id="searchBox" value="" />
  <button type="button" onclick="getData()"><img src="images/magnifying_glass.png" alt="Search" /></button>
</form>

Thanks

Answer:1

use just following simple jQuery

if(event.keyCode == 13){ 
   event.preventDefault();
}

...bind it to you form

Answer:2

I made a code that should highlight searched string but it is not working. Here is the code: <body> <div>div is here</div> <div id="divid"> <div>this is a ...

I made a code that should highlight searched string but it is not working. Here is the code: <body> <div>div is here</div> <div id="divid"> <div>this is a ...

  1. select element with attribute
  2. select element with class
  3. select element with multiple classes
  4. select element with data attribute jquery
  5. select element with jquery
  6. select element with two classes
  7. select element with attribute jquery
  8. select element with javascript
  9. select element with text
  10. select element with class jquery
  11. select element with id css
  12. select element with child
  13. select element with class css
  14. select element with text css
  15. select element with two classes css
  16. select element with multiple classes css
  17. select element with id jquery
  18. select element with class javascript
  19. select element with data attribute javascript
  20. select element with id

I'm making some kind of pinterest backbone application. (post pics/vids/text etc to remember later) It is the first time I try to make a register/login in backbone. I have a homeView and an ...

I'm making some kind of pinterest backbone application. (post pics/vids/text etc to remember later) It is the first time I try to make a register/login in backbone. I have a homeView and an ...

I'm doing JavaScript in node.js. I'm trying wrap an object not yet constructed, allowing calls to any object function and set/get any object attribute through the wrapper, letting the wrapper pass ...

I'm doing JavaScript in node.js. I'm trying wrap an object not yet constructed, allowing calls to any object function and set/get any object attribute through the wrapper, letting the wrapper pass ...

You are programming a lib X that depends on lib Y. You want that library to work on node.js and on the browser. On node.js, you'd use require('Y'). On the browser, you could use require.js. What's the ...

You are programming a lib X that depends on lib Y. You want that library to work on node.js and on the browser. On node.js, you'd use require('Y'). On the browser, you could use require.js. What's the ...

  1. module system of study
  2. module system and services inc
  3. module system of education
  4. module system for c++
  5. module system for javascript
  6. module of system
  7. system module for project