jquery - How to access var from child function in javascript? -


i want id of elements , want use child function

function example(id) {     var me = this;     this.pro = id     this.code = function () {         settimeout(function () {             alert(id)         }, 20)     }     this.validate = function () {         $('.' + id).keyup(function (e) {             var id = this.id;             if (e.keycode == 13) me.code()         })     } } 

body

<input type="text" class="test" id="1" /> <input type="text" class="test1" id="2" />  <script type="text/javascript">     var test = new example('test')     var test1 = new example('test1')     test.validate()     test1.validate() </script> 

either use pro property

function example(id) {     var me = this;     this.pro = null;     this.code = function () {         settimeout(function () {             alert(me.pro);         }, 20);     };     this.validate = function () {         $('.' + id).keyup(function (e) {             me.pro = this.id;             if (e.keycode == 13) me.code();         });     }; } 

or make parameter of function:

function example(id) {     var me = this;     this.code = function (id) {         settimeout(function () {             alert(id);         }, 20);     };     this.validate = function () {         $('.' + id).keyup(function (e) {             var id = this.id;             if (e.keycode == 13) me.code(id);         });     }; } 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -