Click here to Skip to main content
12,510,224 members (52,380 online)
Rate this:
 
Please Sign up or sign in to vote.
Hi all,

I have a form with html text boxes and one submit button. Part of code is like below:
<div>
<input id="add" type="text" style="width: 40%" />
<button id="btnAdd" type="submit" data-bind="click: $root.AddGoal">Add</button>
</div>

But as its a submit button, whenever pressing enter key its calling btnAdd click event. But my need is to call the button click event when only press enter key in #add text control.
Posted 8-Nov-12 21:05pm
akee seth1.6K
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Need to write some javascript code according to your submit button and textbox.
Javascript Code:
<script type="text/javascript">
    function clickSubmitButton(e) {
        if (e.keyCode == 13) {
            document.getElementById("Your submit button id here").click();
        }
    }
</script>

Then need some change in your code:
<div>
<input id="add" onkeypress="clickSubmitButton(event)" type="text" style="width: 40%" />
<button id="btnAdd" type="submit" data-bind="click: $root.AddGoal">Add</button>
</div>

Try this...
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You have a div element with two controls inside. You need to wrap it into a form element to get it to work. Check it out here:
http://www.w3schools.com/tags/tag_form.asp[^]

Good luck!
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160929.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100