[JavaScript]フォームの値を動的に取得する
フォームの値を動的に取得する
全角入力で入力確定前でも
IE8対応
html
<div> <form name="sampleForm" action="" method="post"> <input type="text" name="ps" value="" style="width:300px;"> </form> <div id="result"></div> </div>
js
(function(){ function addEvent(element, type, listener, flg) { if (!element || typeof(element) == "undefined") { return } if (element.addEventListener) { element.addEventListener(type, listener, flg); } else { element.attachEvent('on' + type, function() { listener.call(element, window.event); }); } } function init(){ addEvent(document.sampleForm.ps, ('oninput' in window) ? 'input':'propertychange', function(e){ document.getElementById('result').innerHTML = document.sampleForm.ps.value; }, true); addEvent(document.sampleForm, 'submit', function(e){ e.preventDefault(); }); } addEvent(window,'load',function(){ init(); },true); })();