Christian
Christian

Reputation: 26387

Saving user input directly into variable in JQuery

I don't want to have a formal form field but save every user input directly into a variable and append new letters to a field every time the user enters a new letter. What command do I need?

Upvotes: 1

Views: 320

Answers (1)

Luca Matteis
Luca Matteis

Reputation: 29267

window.onkeypress = function(e) {
    var code;
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    var character = String.fromCharCode(code);
    document.body.innerHTML += character;
};

Upvotes: 1

Related Questions