I have a set of button tags on a webpage and I want to get one particular button tag whose innerText is "Save". (It has no id to it.) So I have this code
var tags = document.getElementsByTagName("button");
for (var i = 0; i < tags.length; i++) {
if (tags[i].innerText === 'Save') {
tags[i].click();
break;
}
}
which works perfectly when I try it in chrome console. But I can't include this in my jelly file(which is an xml markup that will be processed into html; something like a jsp.)
The problem is with the "<" operator in the for loop which is causing this
SAXParserException: "The content of elements must consist of well-formed character data or markup."
And I learnt not to use for..in loops with arrays. What can I do? Please suggest me some workaround.
I have a set of button tags on a webpage and I want to get one particular button tag whose innerText is "Save". (It has no id to it.) So I have this code
var tags = document.getElementsByTagName("button");
for (var i = 0; i < tags.length; i++) {
if (tags[i].innerText === 'Save') {
tags[i].click();
break;
}
}
which works perfectly when I try it in chrome console. But I can't include this in my jelly file(which is an xml markup that will be processed into html; something like a jsp.)
The problem is with the "<" operator in the for loop which is causing this
SAXParserException: "The content of elements must consist of well-formed character data or markup."
And I learnt not to use for..in loops with arrays. What can I do? Please suggest me some workaround.
Share Improve this question edited Apr 19, 2012 at 6:23 Dagg Nabbit 76.8k19 gold badges114 silver badges142 bronze badges asked Apr 19, 2012 at 6:03 VigneshwaranVigneshwaran 3,2756 gold badges25 silver badges36 bronze badges4 Answers
Reset to default 6You are solving the wrong problem. Your problem is "Including a <
character in XML breaks the XML". You need to find out how to include such a character correctly, not avoid having one ever appear in your data. There is no need to avoid a standard for loop.
Either wrap that section with CDATA markers (which stop XML special characters (except the end of CDATA sequence) being special) or represent the <
with <
in the XML.
<![CDATA[
for (var i = 0; i < j; i++) {
etc(i);
}
]]>
or
for (var i = 0; i < j; i++) {
etc(i);
}
You can iterate with the new Array.forEach
method, but it's available from JavaScript 1.6 only:
var tags = Array.prototype.slice.call(document.getElementsByTagName("button"));
tags.forEach(function (tag) {
// ...
});
But the real solution would be to put your code into <![CDATA[]]>
:
<code>
<![CDATA[
var tags = document.getElementsByTagName("button");
for (var i = 0; i < tags.length; i++) {
// ...
}
]]>
</code>
var limit = tags.length;
//loop until 0 (which is false)
while(limit) {
if (tags[tags.length-limit].innerText === 'Save') {
tags[tags.length-limit].click();
break;
}
limit--;
}
Put it inside <[CDATA[ ... ]]>
Editing: Wrong syntax...