Search code examples
javascriptjquerydotnetnuke-5

How to prevent a form to be sent when hitting enter on a none-form input-field? (DNN)


On a page I have a google search-field and a separate form for a login. In order to make the search-field work with enter, I included the following script:

$('#searchBox').keydown(function (event) {
    if (event.keyCode == 13) {
        document.location.href = "someTargetPage.html";
    }
});

The only problem is that in that case the form would be sent because the search-field is included within the form, which I can't change due to the architecture of dot net nuke. I tried to prevent that like this:

$('form').delegate('input:submit', 'click',
    function () {
        return false;
});

Now the search-field does work nicely with enter, but the submit-button from the form won't work! Is there a way to check from where the trigger comes and either allow or deny it?

Thx for any tipps!


Solution

  • Remove your code that stops the input button from working (your delegate on input:submit). You just need to make #searchBox not propagate the event up to the form. It's the search box handler that needs to cancel the event by returning false:

    $('#searchBox').keydown(function (event) {
        if (event.keyCode == 13) {
            document.location.href = "someTargetPage.html";
            return false;
        }
    });