I was using jQuery doing some XML work. Then jQuery told me it can’t find the <col> tag giving me empty data. After having a talk with jQuery, seems like it just doesn’t like to work with <col> XML tags, maybe some expert can explain this to me?
Here’s my XML:
<field>
<property>
<label>Matrix Question</label>
<rows>
<row>row - 1a</row>
<row>row - 2a</row>
<row>row - 3a</row>
<row>row - 4a</row>
</rows>
<cols>
<col>col - 1</col>
<col>col - 2</col>
<col>col - 3</col>
<col>col - 4</col>
<col>col - 5</col>
</cols>
<isrequired>true</isrequired>
</property>
Here’s my code for it:
var xmlWithCol = "<field> <property> <label>Matrix Question</label> <rows> <row>row - 1a</row> <row>row - 2a</row> <row>row - 3a</row> <row>row - 4a</row> </rows> <cols> <col>col - 1</col> <col>col - 2</col> <col>col - 3</col> <col>col - 4</col> <col>col - 5</col> </cols> <isrequired>true</isrequired> </property> </field>";
var xmlWithoutCol = "<field> <property> <label>Matrix Question</label> <rows> <row>row - 1a</row> <row>row - 2a</row> <row>row - 3a</row> <row>row - 4a</row> </rows> <cols> <colm>col - 1</colm> <colm>col - 2</colm> <colm>col - 3</colm> <colm>col - 4</colm> <colm>col - 5</colm> </cols> <isrequired>true</isrequired> </property> </field>"
$(xmlWithCol).find("cols").each(function ()
{
alert($(this).html());
});
$(xmlWithoutCol).find("cols").each(function ()
{
alert($(this).html());
});
as you can see my first output is
col - 1 col - 2 col - 3 col - 4 col - 5
then I found out jQuery doesn’t like <col> tag, I changed it to < colm >< /colm > instead, and it gives me this:
<colm>col - 1</colm> <colm>col - 2</colm> <colm>col - 3</colm> <colm>col - 4</colm> <colm>col - 5</colm>
which is what I want.
How can I make my jQuery love the <col> tag?
I don’t claim to be an expert, but
<col>exists in HTML as an empty element. You can’t give that element text, even if you try to use it as an XML element, because jQuery would be breaking the HTML DOM rules otherwise.Since jQuery was made to work with HTML rather than XML, I can’t think of a good workaround besides simply not using the name
<col>…