Javascript — вернуть вывод консоли / DOM как один вывод JSON
На моей странице я добавил событие onkeyup
, чтобы получить клавиши, которые были нажаты в моем input
.
Я получаю результаты в своей консоли, но мне нужно вывести все события как один вывод JSON
и return
его.
Я новичок в JS
и любая помощь приветствуется
PS: по какой-то причине мой фрагмент здесь не работает, но в моем index.html
я вижу вывод события нормально.
window.onload = function() { const myInput = document.getElementById('myInput'); myInput.onkeyup = function(e) { console.log(e); } }
{amp}lt;input type="text" value="" id="myInput"{amp}gt;
Скриншот ниже — это то, что я получаю взамен:
Токовый выход :
{isTrusted: true, key: "a", code: "KeyA", location: 0, ctrlKey: false, …} {isTrusted: true, key: "s", code: "KeyS", location: 0, ctrlKey: false, …} {isTrusted: true, key: "d", code: "KeyD", location: 0, ctrlKey: false, …} {isTrusted: true, key: "a", code: "KeyA", location: 0, ctrlKey: false, …} {isTrusted: true, key: "s", code: "KeyS", location: 0, ctrlKey: false, …} {isTrusted: true, key: "d", code: "KeyD", location: 0, ctrlKey: false, …} {isTrusted: true, key: "a", code: "KeyA", location: 0, ctrlKey: false, …} {isTrusted: true, key: "s", code: "KeyS", location: 0, ctrlKey: false, …} {isTrusted: true, key: "d", code: "KeyD", location: 0, ctrlKey: false, …}
И то, что я ищу, — это возможность присвоить это переменной, например, var json
и output будет выглядеть примерно так:
{ "onkeyup": "{isTrusted: true, key: "a", code: "KeyA", location: 0, ctrlKey: false, …} {isTrusted: true, key: "s", code: "KeyS", location: 0, ctrlKey: false, …} {isTrusted: true, key: "d", code: "KeyD", location: 0, ctrlKey: false, …} {isTrusted: true, key: "a", code: "KeyA", location: 0, ctrlKey: false, …} {isTrusted: true, key: "s", code: "KeyS", location: 0, ctrlKey: false, …} {isTrusted: true, key: "d", code: "KeyD", location: 0, ctrlKey: false, …} {isTrusted: true, key: "a", code: "KeyA", location: 0, ctrlKey: false, …} {isTrusted: true, key: "s", code: "KeyS", location: 0, ctrlKey: false, …} {isTrusted: true, key: "d", code: "KeyD", location: 0, ctrlKey: false, …}" }
Заранее спасибо!