I want to append an html element to a div and have jquery return me the wrapped set that contains the element I just appended and not the wrapped set containing the div
So my html:-
...
<div id="somediv">Some other elements...</div>
...
javascript:-
var jqueryObj = $('#somediv').append('<p>Hello, World!</p>');
alert(jqueryObj.html());
I'd like this to alert me with 'Hello, world'
not 'Some other elements...<p>Hello, World!</p>'
Element. append() has no return value, whereas Node. appendChild() returns the appended Node object.
append() , the selector expression preceding the method is the container into which the content is inserted. With . appendTo() , on the other hand, the content precedes the method, either as a selector expression or as markup created on the fly, and it is inserted into the target container.
Definition and Usage The appendTo() method inserts HTML elements at the end of the selected elements. Tip: To insert HTML elements at the beginning of the selected elements, use the prependTo() method.
Use .appendTo()
instead of .append()
.
var jqueryObj = $('<p>Hello, World!</p>').appendTo('#somediv');
alert(jqueryObj.html()); // '<p>Hello, World!</p>'
Switch them around, and use the .appendTo()
function. Like so:
var jqueryObj = $('<p>Hello, World!</p>').appendTo('#somediv');
Working DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With