4

I'm trying to create a search option.

If someone is using my search field, they can press the "Search-Button" or just press the enter key to start the function.

My problem is that if they press the enter key within the text box it starts my function and after the function it calls the button click.

I've used Google but couldn't solve it, even tried to disable the button while myFunc() is called.

<!DOCTYPE html>
<html>
    <body>
        <form>Search
            <br>
            <input type="text" id="searchField" onKeyDown="if(event.keyCode==13) myFunc()">
            <br>
            <button type="button" id="myButton" onClick="myFunc()">Search</button>
        </form>
    </body>
</html>
3
  • Where is the myFunc() code? The most important part of your question Commented Oct 28, 2015 at 16:11
  • 1
    @Jesse the function its self has nothing to do with the question. Commented Oct 28, 2015 at 16:23
  • Alright. My mistake. I must have read it too fast. Commented Oct 28, 2015 at 16:24

5 Answers 5

1

Looks like you want to stop the event propagation in this case. I've changed your code to the following:

<!DOCTYPE html>
<html>
<body>

<form>
  Search<br>
  <input type="text" id="searchField" onKeyDown="if(event.keyCode==13){console.log('Hello World from Input'); return false;}">
  <br>
  <button type="button" id="myButton" onClick="console.log('Hello World from Button')">Search</button>
</form>
</body>
</html>

Pressing enter while the input element has the focus gives me only Hello World from Input at the console.

Sign up to request clarification or add additional context in comments.

Comments

1

I think this is what you are asking for: https://jsfiddle.net/7vf9pz8u/1/

HTML

<form>Search
    <br>
    <input type="text" id="searchField" onKeyDown="pressEnter()">
    <br>
    <button type="button" id="myButton" onClick="myFunc()">Search</button>
</form>

JavaScript

function pressEnter() {
    if (event.keyCode == 13) {
        alert("working");
    }
}

1 Comment

Something like that just even if I handle it with two functions both getting called. As I used Google I saw an article which said if they use the keyDown within the textfield it clicking also the first button in the form
1

Handle the onKeyDown separately in a function (onEnter) and use event.preventDefault(); block the default action.

See the below code.

window.onEnter = function () {
    if (event.keyCode == 13) {
        event.preventDefault();
        return false;
    }
}
<form>
  Search<br>
  <input type="text" id="searchField" onKeyDown="onEnter()">
  <br>
  <button type="button" id="myButton" onClick="myFunc()">Search</button>
</form>

http://jsfiddle.net/kishoresahas/cvyzLdy8/

2 Comments

Strange your example working perfeclty as it should. Mine doesn't even just got an alert in myFunc()
but in my example I have defined onEnter function . do u mean that ?
0

I think you should prevent form submitting by event.preventDefault() in onkeydown function.

Comments

0

I have tested your code in chrome and firefox browser and it is working perfectly as you wish, I make some update in your code to show that it is actually working fine. Follows:

<!DOCTYPE html>
<html>
<body>
<script>
function myFunc(){
    alert("Typed text.: "+document.getElementById("searchField").value) 
}
</script>
<form>
  Search<br>
  <input type="text" id="searchField" onKeyDown="if(event.keyCode==13) myFunc()">
  <br>
  <button type="button" id="myButton" onClick="myFunc()">Search</button>
</form>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.