I am attempting to create a console. Let's say I a function foo()
and I enter it into the "console". The console should run that function. I attempted the following:
var $console = $('.console');
var console = {
log: function(txt) {
$console.append(txt + '<br>');
},
clear: function() {
$console.text(' ');
}
}
function foo() {
console.log('foo ran');
}
var bKeys = [];
$('body').keydown(function(event) {
if (event.which === 13) {
//run the provided function from the input
($('.run').text())();
}
});
.console {
position: absolute;
right: 0px;
top: 0px;
background-color: #fff;
width: 300px;
height: 100%;
box-shadow: 0px 0px 5px #444;
overflow-y: auto;
}
.run {
position: fixed;
bottom: 0px;
width: 295px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='console'><input class='run' type='text' placeholder='Run a command...'/>Console: <br> </div>
What I tried was returning the text and trying to run that function after Enter is pressed in the <input>
. However, that isn't working. How can I run a typed function from <input>
?