1
I created a div.box in html and I want to insert another div that I create in the execution of the code, follow the avaixo:
HTML:
<div class="box"></div>
JS:
var item = $('<div />', {class: 'item'})
var box = $('.box')
box.on('click', function (e) {
this.append(item)
})
But instead of showing div.item inside div.box appears the text [Object Object]. I realized that this only happens when run in the click event, doing the way below works:
var item = $('<div />', {class: 'item'})
var box = $('.box')
box.append(item)
Someone has an explanation for this?
When you use the
this
within the.on()
error occurs, but what if you switch tobox.apend(item);
within the.on()
the error persists?– Thiago Santos
This does not answer the question, but another thing one can do instead of using append is to concatenate the innerHTML of the elements with what was before + the new content - This is not the most efficient mode, but sometimes it is the simplest.
– Antonio Alexandre