Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append is not a function jquery

In the following code, I'd like to be able to append test_div to the element that was clicked on. However, I get the error message that TypeError:e.srcElement.appendis not a function.

$(document).ready(function() {

    onclick = function (e) {
        var test_div = document.createElement("div");
        target = e.srcElement
        target.append(test_div);
        }

    $('#dialogue').on('click', onclick);

});

I've tried printing out the type of e.srcElement, but it just says object, which isn't very helpful. What's the correct way to append an element to the item clicked on?

like image 543
Everyone_Else Avatar asked Nov 28 '22 14:11

Everyone_Else


1 Answers

.append() is jquery method. You need to convert DOM object into jquery object for using jquery methods:

 $(target).append(test_div);
like image 128
Milind Anantwar Avatar answered Dec 11 '22 05:12

Milind Anantwar