I have a namespace called "X" and it has a getJSON function that fetches some file from the server. I want to reuse this namespace with varying contents. Here is the code:
var uid = "27D76901-B6B1-B332-918B-D9F5A7152EFC";
var X = {
el: "",
elMarkup: "",
url: "",
initialize: function(el, elMarkup, url){
$.getJSON(url, function(data){
el.find("p").remove().end();
$.each(data.comments, function(key, value){
el.append(elMarkup);
})
})
}
};
X.el = $("#comments");
X.elMarkup = "<p>"+ value.content +" by "+ value.user +"</p>";
X.url = "http://example.com/getComments?uid=" + uid;
X.initialize(X.el, X.elMarkup, X.url);
I am having a problem with the elMarkup variable for the $.each loop. How can I define this variable dynamically? Thanks!
X.elMarkup = "<p>"+ value.content +" by "+ value.user +"</p>";will give youundefinedfor both variables as they are not defined outside the function.