Nich Nich - 1 year ago 41
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.

), in a string (contents) but the append function does not append to the found jQuery object even if
returns the right jQuery object.

var nodeWorkShift = $('<div>' + stringWorkShift + '</div>');

Answer Source

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') );