1

This is what I'm trying to do:

$('<abc/>').append('<x/>').html()

I expect it to be: "<abc><x/></abc>". Am I wrong?

yegor256
  • 102,010
  • 123
  • 446
  • 597

5 Answers5

2

You can hug your elements in a parent container as such:

$('<div>').append($('<abc/>').append('<x/>')).html()

You'll get the result you want:

http://jsfiddle.net/tuTAK/2/

Marty Cortez
  • 2,325
  • 1
  • 17
  • 22
  • Thanks, you're right. But it doesn't work with XML+XSL. Looks like my problem is somewhere else. – yegor256 Mar 24 '12 at 05:20
  • this is the full problem: http://stackoverflow.com/questions/9834487. if you add your code there you will see the problem – yegor256 Mar 24 '12 at 05:24
0

You should be adding an id attribute to your abc tag

<abc id="AbcId">

and referencing it like this:

$('#AbcId').append('<x/>')
nightshifted
  • 538
  • 4
  • 7
0

.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>
craftsman
  • 15,133
  • 17
  • 70
  • 86
0

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

found in this fiddle i made for another answer

SpYk3HH
  • 22,272
  • 11
  • 70
  • 81
0
$("<abx>").append("<x/>")[0].outerHTML

result :

"<abx><x></x></abx>"

.html() only outputs the content of

$("<abx>")
mpm
  • 20,148
  • 7
  • 50
  • 55