(Full code below)
This
$('#' + id).parent().append('<div id="pop-up">hello</div>');
does work. But this
$('#' + id).parent().append('<div id="pop-up-' + id + '">hello</div>');
doesn't.
The fact that the first version works lets me assume that the problem is not the id variable...
So the full code is
function clickOnElement(id) {
var obj = {};
obj.clicked = String(id);
var jsonData = JSON.stringify(obj);
$.ajax({
type: 'POST',
url: '../db/RequestHandler.ashx',
data: jsonData,
contentType: 'application/json; charset-utf-8',
success: function (response) {
// Add the div for the dialog-box
$('<div>Hello</div>', {
"id": "pop-up" + id
}).appendTo($('#' + id).parent());
},
error: function () {
console.log("request error");
}
});
}
.appendexpects an element and not string. If you have HTML string, use.html()instead.append()will work after the DOM load. parent div is loaded but concurrently you are appending to that element which is not in DOM at that time. try to append after the document ready$('#@r.id').click(function() { clickOnElement(@r.id); });