Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

I have a form with html text boxes and one submit button. Part of code is like below:
HTML
<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

Need to write some javascript code according to your submit button and textbox.
Javascript Code:
JavaScript
<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:
XML
<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...
 
Share this answer
 
v2
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!
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900