jquery - How to inherit Class in javascript? -
the function not working, not putting values in "input".
function example(id){ this.code= function(){$('.'+id).val(id)} this.validate= function (){ $('.'+id).keyup(function (e){ if(e.keycode==13) this.code(); }) } }
body
<input type="text" class="test"/> <input type="text" class="test1"/> <script type="text/javascript"> var test= new example('test') var test1= new example('test1') test.validate() test1.validate() </script>
your keyup event handler have different "this". can use bind correct problem
keyup(function (e){ if(e.keycode==13) this.code(); }.bind(this))
when bind event, handler called "this" keyword set object fired event, not object created event.
Comments
Post a Comment