0

I tried creating new tags using jQuery and appending using $("<div></div>"). But I want to create some XML elements which are orphan tags (which has no ending tags).

When I tried to create such tags, using

$("<column/>")

It is creating

<column></column>

and I am expecting <column />.

How can I do this without any jQuery plugin? I am adding some attributes for it after creating that element.

Robert Longson
  • 118,664
  • 26
  • 252
  • 242
Laxmikant Dange
  • 7,606
  • 6
  • 40
  • 65

2 Answers2

0

The documentation says:

When the parameter has a single tag, such as $('<img />') or $('<a></a>'), jQuery creates the element using the native JavaScript createElement() function.

So it is the browser that decides whether the tag is a self-closing tag or there should be a closing tag.

If you are definitely sure that it doesn't have a self closing tag, like the <circle /> element and it has a specification, then you need to use:

var circle = document.createElementNS("http://www.w3.org/2000/svg", "circle");

Reference: Creating SVG graphics using Javascript?

Community
  • 1
  • 1
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

After doing some research, I found following answer.

Its little difficult to create XML document using jQUery, but with JavaScript, it is very easy.

Create instance of xml document.

var createXMLDocument=function(){
 var _doc,perser;
 if(window.DOMParser){
   parser=new DOMParser();
   _doc=parser.parseFromString("<doc></doc>","application/xml");
 }else{
   _doc=new ActiveXObject("Microsoft.XMLDOM");
   doc.async="false";
   _doc.loadXML("<doc></doc>");
 }
 return _doc
}

Create elements.

var xml=new createXMLDocument();
var _columns=xml.createElement("columns");
var _column=xml.createElement("column");

Attach it to XML main document.

_columns.appendChild(_column);
xml.firstChild.appendChild(_columns);

It will create xml like this;

<doc>
  <columns>
    <column/>    // This is what I needed
  </columns>
</doc>
Laxmikant Dange
  • 7,606
  • 6
  • 40
  • 65