How can I do something like a console from http://codecademy.com in JS / PHP?

    1 answer 1

    Hi, I do not pretend to be an elegant solution at all, but I tried to do this:

    HTML: <section><input /></section> JS: $('input:last').focus(); $('section').on('keyup', function(e) { if (e.which == 13) { e.preventDefault(); $('input:last').attr('disabled','disabled'); $('section').append('<div class="msg">Hello! Please gimme a work, masta!</div>').append('<input />'); $('input:last').focus(); } }); 

    Maybe someone will like the idea.

    Example: http://jsfiddle.net/3C4j8/

    PS> for some reason the $ ('input') event did not work on dynamically added Input. On ('keyup', callback); I had to “crutch” from the whole section, can someone tell me why?

    • one
      Due to the fact that at the moment when the event is hung, the input does not exist and therefore the event is not hung on it. Previously, for this, yuzal .live (), now I use delegation: jsfiddle.net/3C4j8/1 - MrFranke
    • Thank you very much, I’ll try to dig into this :) I saved it all in one html (css and js indicated in the head), I open it in the browser - and the console is no longer interactive, you can’t enter anything. Why? - goudwine
    • JS Wrap in a function $(document).ready(function (){ /* код */ }); - ferrari
    • Did not help. troloload.ru/f/12729_1.html Tell me what's wrong? - goudwine
    • @goudwine, you have not connected the library itself. Above your JS code, add: <script src = "// ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </ script> - Deonis