Nich Nich - 3 months ago 9
HTML Question

How can I append html to a found selector in an HTML string with jQuery?

I am trying to append a node to a found jQuery object that was found with a selector, which is in this case an id (selector, e.g.

#emp1-day3
), in a string (contents) but the append function does not append to the found jQuery object even if
$(contents).find(selector)
returns the right jQuery object.

var nodeWorkShift = $('<div>' + stringWorkShift + '</div>');
$(contents).find(selector).append(nodeWorkShift);

Answer

If contents is a string then your code effectively doesn't do anything as the created and modified collection is thrown away. You need to store the collection.

var nodeWorkShift = $('<div>' + stringWorkShift + '</div>');
var $contents = $(contents).find(selector).append(nodeWorkShift).end();
// string representation of the collection
console.log( $contents.prop('outerHTML') );