javascript - AJAX Putting HTML into a div that's created in the same code -
i have problem code, , have searched hours fix, no luck. tried many things myself, no luck. here's problem:
i have js function creates new 'window', including div's, dynamically created. have ajax request in function, gets data php file, , supposed put data in corresponding div same function creates. error:
uncaught typeerror: cannot set property 'innerhtml' of undefined
my code:
var prowindow = function(height,width,title,type,id){ this.max = false; this.name = title; this.mmain = document.createelement("div"); this.mmain.classname = "main"; this.appid = id; this.id = this.name + id; $.ajax({ url: 'includes/getappcontents.php', type: 'get', data: {thisappid: this.appid}, }) .done(function(result) { var result = $.parsejson(result); var code = (result['code']); this.mmain.innerhtml = code; }) .fail(function() { //console.log("failed retrieving code application"); }) .always(function() { //console.log("initializing application"); });
the 'this' part of code lost in ajax brackets, causes problem, ajax doesn't know mean with: "this.mmain", how supposed fix that?
thanks, -tristan
the problem you're losing context. once enter done
callback this
no longer points outer this
define element.
you can rectify telling jquery call callback in outer context, via bind()
(or jquery's .proxy()
):
.done(function(result) { var result = $.parsejson(result); var code = (result['code']); this.mmain.innerhtml = code; }.bind(this))
Comments
Post a Comment