JQuery: subscripts of xml object names

jQuery reduces all values ​​of the name attributes.
I have an HTML form:

 <input type="text" name="firstName"> <input type="text" name="lastName"> <input type="text" name="amountForName"> 

Trying to read it and create xml with its value.
jQuery

 $(section).find('input').each(function(i, field) { console.log($(field).attr('name'));//prints 'firstName' var $fieldName = $.createElement($(field).attr('name')); $fieldName.text($(field).val()); $.createElement = function(name) { console.log('Creating Element '+name); //prints 'firstName' return $('<'+ name +' />'); } 

But a free wrapper appeared in the xml elements, and I get

 <firstname>himanshu</firstname><lastname>yadav</lastname> 

How can I save camel shell for node names?

+4
source share
2 answers

Check here:

 xmlDoc = document.implementation.createDocument("", "", null); root = xmlDoc.createElement("description"); xmlDoc.appendChild(root); alert((new XMLSerializer()).serializeToString(xmlDoc)); 

This is from here: fooobar.com/questions/1284908 / ...

Also check out here: http://www.w3schools.com/dom/dom_domdes_create.asp

+2
source

jQuery is designed to parse and process HTML, not XML. Use the XML library to create XML if that is what you want, or create it in a string.

 var result=''; $(section).find('input').each(function(i, field) { console.log($(field).attr('name'));//prints 'firstName' var $fieldName = $(field).attr('name'); result+='<'+$fieldname+'>'+$(field).val()+'</'+$fieldname+'>'; } 

This, for example, is only. In a real production system, you need to make sure that the field name and value are encoded in XML format, otherwise all kinds of bad things can happen.

+1
source

Source: https://habr.com/ru/post/1495002/


All Articles