While typing in a text input field, printing the content typed in a div

前端 未结 5 2082
时光取名叫无心
时光取名叫无心 2020-12-13 03:08

I have a website where there is a empty box and a input text box. I want to be able to type something in that input box and have it be printed on the empty box.

HTM

相关标签:
5条回答
  • 2020-12-13 03:24

    Angular JS does this in two lines of code :

    Just import Angular JS as you import other libraries :

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js">
    

    Then, on you first div (where you are copying from):

    <input type="text" ng-model="myID"> </input>
    

    Then, on the place where you will show the content : just write :

    <div> {{myID}}</div>
    

    This is the best solution I have ever found !

    0 讨论(0)
  • 2020-12-13 03:26

    http://jsfiddle.net/3kpay/

    <div class='printchatbox' id='printchatbox'></div>
    
    <input type='text' name='fname' class='chatinput' 
        onkeyUp="document.getElementById('printchatbox').innerHTML = this.value" />
    
    0 讨论(0)
  • 2020-12-13 03:33

    You use the onkeyup event

    Searching with ids is a lot easier. Add ids to your elements as follows:

    <div class='printchatbox' id='printchatbox'></div>
    
    <input type='text' name='fname' class='chatinput' id='chatinput'>
    

    JS

    var inputBox = document.getElementById('chatinput');
    
    inputBox.onkeyup = function(){
        document.getElementById('printchatbox').innerHTML = inputBox.value;
    }
    

    Here is a Live example

    0 讨论(0)
  • 2020-12-13 03:34

    In your HTML,

    <div id='printchatbox'></div>
    <br>
    <input type='text' id='fname' class='chatinput' onkeyup="annotate()">
    

    In JS,

    function annotate(){
      var typed= document.getElementById("fname").value;
      document.getElementById("printchatbox").innerHTML= typed;
    }
    

    Click here for LIVE DEMO

    0 讨论(0)
  • 2020-12-13 03:40

    There are many ways to get this done, possibly the easiest is to use jQuery. In the example below I am using the jQuery keyUp() function to listen for keyboard events, then writing the updated value to the .printChatBox

    <!DOCTYPE html>
    <html>
    <head>
    <script src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
    </head>
    <body>
    
      <div class='printchatbox'>CHANGE ME</div>
      <input type='text' name='fname' class='chatinput'>
    
    <script type="script/javascript">
      $('.chatinput').keyup(function(event) {
        newText = event.target.value;
        $('.printchatbox').text(newText);
      });
    </script>
    </body>
    </html>
    

    I've posted a working example here: http://jsbin.com/axibuw/1/edit

    0 讨论(0)
提交回复
热议问题