0

Does anyone know how to make submit button on enter in my case?

<div data-bind="with: idea">
    <input type="text" data-bind='value:itemToAdd, valueUpdate: "afterkeydown"' />
    <input type="button" id="addButton" data-bind="click:$parent.addItem, enable: itemToAdd" value="add" />
    <ul data-bind="foreach:allItems">
        <li> <span data-bind="text:$data"></span>
                <input type="button" data-bind="click:$root.removeItem.bind($parent, $data)" value="-" />
        </li>
    </ul>
</div>

Here's fiddle

Igor Dymov
  • 16,230
  • 5
  • 50
  • 56
mk_yo
  • 752
  • 1
  • 12
  • 39

1 Answers1

2

I would recommend to implement it this way:

<form data-bind="with: idea, submit: addItem">
    <input type="text" data-bind='value:itemToAdd, valueUpdate: "afterkeydown"' />
    <input type="submit" id="addButton" data-bind="enable: itemToAdd" value="add" />
    <ul data-bind="foreach:allItems">
        <li> <span data-bind="text:$data"></span>
                <input type="button" data-bind="click:$root.removeItem.bind($parent, $data)" value="-" />
        </li>
    </ul>
</div>

The idea is following: you're wrapping your controls with the form and adding submit binding to it, that means action, that will be performed on form submit. And now on pressing Enter inside the form you are actually submitting the from, that's why submit action will be performed.

Igor Dymov
  • 16,230
  • 5
  • 50
  • 56