Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically created p element not appended to dynamically created div

By some reason the p element in the function below isn't created (and/or appended to the div). If I append it to '#listOfListObjects' as with the div, it works, but I want it inside the div.

What am I doing wrong?

    $('#addListObjectSubmit').click(function (e) {

        var listObjectName = $('#m_newListObject').val();

        if((listObjectName == null) || (listObjectName == '')) {
            return false;
        }
        else {
            var listDiv = 'listDiv' + i;

            $('<div>', {
                class: 'listObjectShow',
                id: listDiv
            }).appendTo('#listOfListObjects');

            $('<p>', {
                class: 'listObjectShow',
                text: listObjectName,
                id: 'listObject' + i
            }).appendTo(listDiv);
        }

        i += 1;

        e.preventdefault();
    });
like image 676
holyredbeard Avatar asked Dec 28 '25 16:12

holyredbeard


1 Answers

Change this

var listDiv = 'listDiv' + i;

to

var listDiv = '#listDiv' + i;

or

$('<div>', {
    class: 'listObjectShow',
    id: listDiv
}).appendTo('#listOfListObjects');

$('<p>', {
    class: 'listObjectShow',
    text: listObjectName,
    id: 'listObject' + i
 }).appendTo(listDiv);

To

$('<div>', {
     class: 'listObjectShow',
     id: listDiv
 }).appendTo('#listOfListObjects');

 $('<p>', {
     class: 'listObjectShow',
     text: listObjectName,
     id: 'listObject' + i
 }).appendTo('#' + listDiv);
like image 184
Om3ga Avatar answered Dec 30 '25 05:12

Om3ga