1

(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");
        }
    });
}
5
  • .append expects an element and not string. If you have HTML string, use .html() instead Commented Oct 7, 2016 at 6:21
  • .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 Commented Oct 7, 2016 at 6:26
  • works perfeclty fine for me jsfiddle.net/gx6mrqr7 Commented Oct 7, 2016 at 6:41
  • where is the id coming from? Commented Oct 7, 2016 at 6:47
  • I am developing in ASP.NET Razor: $('#@r.id').click(function() { clickOnElement(@r.id); }); Commented Oct 7, 2016 at 6:48

3 Answers 3

2

Use .appendTo()

  • The .append() and .appendTo() methods perform the same task.
  • The major difference is in the syntax-specifically, in the placement of the content and target.
  • With .append(), the selector expression preceding the method is the container into which the content is inserted.
  • With .appendTo(), on the other hand, the content precedes the method, either as a selector expression or as markup created on the fly, and it is inserted into the target container.

Code Example

$('<div>Hello</div>', {
    "id": "pop-up" + id
}).appendTo($('#' + id).parent());

FYI,

Please make sure the element exists in the DOM and you're appending it in correct selector.

Hope this helps.

Sign up to request clarification or add additional context in comments.

7 Comments

Yes, you made a mistake with the ' but I corrected it. This $('#' + id).parent().append("<div id='pop-up" + id + "'>hello</div>"); seems to work. Can anyone explain why??
I've updated my answer, just make sure that your use appendTo at right one.
there's nothing wrong with .append() it's just a quotation mark confilct.
Thank you for your answer. The div is appended correctly now. Only the id attribute is not working yet. But maybe I could do a workaround with the .attr() method of jQuery.
Okay, Glad to see that it finally helps you.
|
1

Try the following

var parent = $('#' + id).parent();
var el = $('<div>', {id: "pop-up"+id,text:"hello"});
 el.appendTo(parent);

the proper way to add text to a created object is to use the text property:

demo:https://jsfiddle.net/gx6mrqr7/

4 Comments

This actually worked. Thank you. Any suggestions why?
from what i can tell jquery can't add property to a closed div <div>Hello</div>
I also noticed that "pop-up-" + id doesn't work, but "pop-up" + id does.
Well, I'm not relying on the - ;-)
1

Create HTML using jQuery(html, attributes), You will be safe from quotes mess

$('#' + id)
    .parent()
    .append($('<div>', {
        id: "pop-up-" + id ,
        text: "hello"
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.