Search code examples
javascripthtmllive

make javascript variable update live with text input


hey this is an edited version of my first post:

Pretty much what I want to do is have a text input box that stores its contents in a javascript variable. The catch is I want it to happen live, so the variable will update automatically say, ever .2 seconds without the user needing to press a submit button. Thanks


Solution

  • Are you looking for the two way data binding? Something like this: https://codepen.io/manishiitg/embed/ZYOmbB?

    <html>
        <head>
            <script src="https://code.angularjs.org/1.3.8/angular.min.js"></script>
            <script>
                var mainMod = angular.module('MainApp', []);
                mainMod.controller('MainCtrl', function ($scope) {
                    $scope.text = '';
                });
            </script>
        </head>
        <body ng-app='MainApp'>
            <div ng-controller='MainCtrl'>
              <div>
                Change Text Here: 
                <input type='text' ng-model='text' />
              </div>
              <div>
                <p>Text: {{text}}</p>
              </div>
            </div>
        </body>
    <html>
    

    You can achieve that with Angular, React and Vue.js framework, even with jQuery. Perhaps you should try one of the frameworks that solves your needs quickly.

    If you choose Angular, I recommend you to make the tutorial steps, specially this part: https://angular.io/tutorial/toh-pt1#two-way-binding