Click here to Skip to main content
11,644,731 members (67,175 online)
Rate this: bad
good
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
0 OriginalGriff 680
1 jyo.net 494
2 Sergey Alexandrovich Kryukov 464
3 Afzaal Ahmad Zeeshan 424
4 CPallini 386
0 OriginalGriff 1,020
1 Sergey Alexandrovich Kryukov 673
2 DamithSL 656
3 Afzaal Ahmad Zeeshan 652
4 CPallini 555


Advertise | Privacy | Mobile
Web03 | 2.8.150731.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2015
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