This is what I'm trying to do:
$('<abc/>').append('<x/>').html()
I expect it to be: "<abc><x/></abc>"
. Am I wrong?
This is what I'm trying to do:
$('<abc/>').append('<x/>').html()
I expect it to be: "<abc><x/></abc>"
. Am I wrong?
You can hug your elements in a parent container as such:
$('<div>').append($('<abc/>').append('<x/>')).html()
You'll get the result you want:
You should be adding an id attribute to your abc tag
<abc id="AbcId">
and referencing it like this:
$('#AbcId').append('<x/>')
.html() returns child elements of the selected object. In your case, it should return this:
<x></x>
If you want to get out html, you can write a quick function:
$.fn.outerHTML = function() {
return $('<div>').append( this.eq(0).clone() ).html();
};
Now if you re-write your code with outerHTML():
$('<abc/>').append('<x/>').outerHTML();
You will get this:
<abc><x><x/></abc>
in that case, i guess what you have is close to correct, for instance, build a div with a header in it that says "Title";
$("<div />").append($("<h1 />").text("Title"))
or a div with a p element with a link in it after some text
$("<div />").append($("<p />").text("Mail Me Here:").append($("<a />").attr("href", "mailto:somemail@gmail.com").text("someMail@Gmail.com")))
append it to the body with
...eMail@Gmail.com"))).appendTo($("body"))
Keep in mind you can also .prepend and .prependTo elements
See an example of appending new table rows from data as
$("<abx>").append("<x/>")[0].outerHTML
result :
"<abx><x></x></abx>"
.html() only outputs the content of
$("<abx>")