locked
how to call a function on pressing ENTER of a dynamically created input element. RRS feed

  • Question

  •  I just need to accept the value in the text box when enter key is pressed. But seems so tough to achieve.  I am trying to call the additemkey function when enter key is pressed in the text box.

    var x = document.createElement("input");
            x.setAttribute("id", string);
            x.setAttribute("type", "text");
            x.setAttribute("style", "max-width:207px;");
            x.addEventListener(onkeypress, additemkey(this), false);

     In additemkey function I am trying to check if e.which is 13 (return key)  and if so, call the actual add item function.

    Is this the right approach? Thanks so much.


    karthika

    Monday, May 13, 2013 7:50 AM

Answers

  • yes, thats correct. Make sure to return false from your function or else you will not prevent the default handling of return key in the input field (usally a ding sound is played).
    • Marked as answer by ArunKarthika Monday, May 13, 2013 3:43 PM
    Monday, May 13, 2013 2:15 PM

All replies

  • yes, thats correct. Make sure to return false from your function or else you will not prevent the default handling of return key in the input field (usally a ding sound is played).
    • Marked as answer by ArunKarthika Monday, May 13, 2013 3:43 PM
    Monday, May 13, 2013 2:15 PM
  •  

    Thanks.  "return false;" did it all.


    karthika

    Monday, May 13, 2013 3:44 PM