I tried to add styling to a dynamically created element, and found this good working solution here on the site.
-----EDIT: adding more code -----
function createAndShowPatch(){
selected = $('.active');
var radiosForPatch = [];
for (radio in selected){
//must be a beeter way to iterate over "selected"
//without checking if Object and has property "id"... any suggestions?
if (typeof selected[radio] === "object" && "id" in selected[radio]){
radiosForPatch.push(($.grep(radios, function(e){ return e.id == selected[radio].id; }))[0]);
}
}
var patch = new Patch(radiosForPatch);
var guiPatch = createNewGUIPatch(patch);
var style = {
position: "absolute",
top: mouseY +"px",
left: mouseX +"px"
}
$(guiPatch).appendTo($(".container")).css(style).draggable();
}
function createNewGUIPatch(patch){
var patchToReturn;
var patchContent = "";
for (radio in patch.radios){
patchContent += "<p> " + patch.radios[radio].name + "</p>";
}
patchToReturn = "<div class='patch'><h1>Patch</h1>" + patchContent + "</div>";
return patchToReturn;
}
-------End of edit ------ But that made me wonder why what I tried didn't work and think maybe I am missing some basic important thing related to JQuery (or even javascript :-/ )
so , the question is, what is the difference between this:
$(guiPatch).appendTo($(".container")).css(style);
and this (splitting to 2 lines):
$(guiPatch).appendTo($(".container"));
$(guiPatch).css(style);
and why the first one is working while the second one didn't.
Thanks :).
stylevalue?guiPatchis a piece of HTML markup, each call to$(guiPatch)creates new DOM elements.$(guiPatch)[0] !== $(guiPatch)[0].