title: Associate a string with a function name
body: <p>I have a text input that I want to enable users to call functions from.</p>
  
  <p>Essentially I want to tie strings to functions so that when a user types a certain 'command' prefaced with a backslash the corresponding function is called.</p>
  
  <p>Right now for example's sake you can type <code>/name</code>, followed by a value and it will set name as a property of the user object with the value the user gives.</p>
  
  <p>So how would I do this with 20 or so 'commands'?</p>
  
  <p><a href="http://jsfiddle.net/k7sHT/5/" rel="nofollow">http://jsfiddle.net/k7sHT/5/</a></p>
  
  <p>jQuery:</p>
  
  <pre class="lang-js prettyprint-override"><code>$('#textCommand').on('keypress', function(e) {
          if(e.keyCode==13) {
              sendConsole();
          }
  });
  
  var user = {};
  
  var sendConsole = function() {
      value = $('#textCommand').val();
      if (value.substring(0,5) === "/name") {
          user.name = value.substring(6,20);
          alert(user.name);
      } else {
          $('body').append("&lt;span&gt;unknown command: "+value+"&lt;/span&gt;&lt;br /&gt;")
          $('#textCommand').val("");
      }
  }
  </code></pre>
  
  <p>HTML:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;input id="textCommand" type="text"&gt;&lt;br/&gt;
  </code></pre>