Stop page refresh when 'enter' is pressed in input text element

Is there a way to completely disable a webpage when the enter button is pressed in an input text element?

I am looking to create a search field in which I can get text when I type in to filter objects and display only those that contain text from the search field.

I tried the following to try to catch the enter button, but it does not work.

function setupSearchField() {
    document.getElementById("searchField").onKeyDown = function(event) {
        var holder;
        if (window.event) {
            holder = window.event.keyCode;
        } else {
            holder = event.which;
        }
        keyPressed(holder);
    }
}

function keyPressed(key) {
    if (key == 13) {
        event.cancelBubble = true;
        return false;
    }
}
+3
source share
4 answers

If the input element is inside the form and this form is not actually sent to the server, delete the form.

, , , onkeydown , ( return keyPressed(holder); - keyPressed setupSearchField, ).

+5

Is your search box inside an element? Then pressing 'enter' triggers a submit event to the form element.

In this case, you can process your filtering by specifying onsubmit on the form element.

<form id="searchForm">
    <input type="text" name="search" />
</form>
<script>
    document.getElementById('searchForm').onsubmit = function() {
        var searchValue = this.search.value;
        // process
        return false;
    }
</script>

Something like this is possible.

0
source

Just add the following javascript code to your Visualforce page:

<script type='text/javascript'>
function stopRKey(evt) 
{
   var evt=(evt) ? evt : ((event) ? event : null);
   var node=(evt.target)?evt.target:((evt.srcElement)?evt.srcElement:null);
   if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}

document.onkeypress = stopRKey;
</script>
0
source

All Articles