jquery - Javascript access object variables from functions -
function init_exam_chooser(id,mode) { this.id=id; this.table=$("#" + this.id); this.htmlroworder="<tr>" + $("#" + this.id + " tbody tr:eq(0)").html() + "</tr>"; this.htmlrownew="<tr>" + $("#" + this.id + " tbody tr:eq(1)").html() + "</tr>"; $("#" + this.id + " tbody tr").remove(); //arxikopoiisi var rownew=$(this.htmlrownew); rownew.find("input[type='text']").eq(0).autocomplete({ source: function (req,resp) { $.ajax({ url: "/medilab/prototypes/exams/searchquick", cache: false, datatype: "json", data:{codename:req.term}, success: function(data) { resp(data); } }); }, focus: function(event,ui) { return false; }, minlength :2 }); rownew.find("input[type='text']").eq(1).autocomplete({ source: function (req,resp) { $.ajax({ url: "/medilab/prototypes/exams/searchquick", cache: false, datatype: "json", data:{name:req.term}, success: function(data) { resp(data); } }); }, focus: function(event,ui) { return false; }, minlength :2 }); rownew.find("input[type='text']").bind( "autocompleteselect", function(event, ui) { alert(htmlroworder); var row=$(htmlroworder); $(table).find("tbody tr:last").before(row); alert(ui.item.id); }); rownew.appendto($(this.table).find("tbody")); //this.htmlrownew }
the problem @ ,how can access htmlroworder? tried this.htmlroworder , didnt work.... ideas??
rownew.find("input[type='text']").bind( "autocompleteselect", function(event, ui) { alert(htmlroworder); var row=$(htmlroworder); $(table).find("tbody tr:last").before(row); alert(ui.item.id); });
your issue this
not think inside event handlers. jquery event handlers run in context of element on event triggered; means inside event handler, this
element on event triggered.
you can solve problem either waiting next revision of javascript, have function.prototype.bind baked in, or setting reference scope object outside event handler , referring inside, patrick's answer.
function(){ var instance = this; this.foo = "abc123"; $('someselector').click(function(ev){ this.foo; //causes error; foo element matching 'someselector' instance.foo; //this returns "abc123" } }
Comments
Post a Comment