2

I have a javascript code in my xhtml file. Here it is:

<h:head>
    <h:outputStylesheet library="css" name="styles.css"  /> 
     <script>
            function dataTableSelectOneRadio(radio) {
                var id = radio.name.substring(radio.name.lastIndexOf(':'));
                var el = radio.form.elements;
                for (var i = 0; i < el.length; i++) {
                    if (el[i].name.substring(el[i].name.lastIndexOf(':')) == id) {
                        el[i].checked = false;
                    }
                }
                radio.checked = true;
            }
        </script>
</h:head>

The problem ist, inside the for loop it gives error for "<" operator saying that "The content of elements must consist of well-formed character data or markup.". I think it sees "<" as an html element i.e. "body, html etc. but i am not sure. How can i fix this?

Thanks

Edit: I now tried this but still an error. Is there a syntax error you see?

<script>
            function dataTableSelectOneRadio(radio) {
                var id = radio.name.substring(radio.name.lastIndexOf(':'));
                var el = radio.form.elements;
                for (var i = 0; i &lt; el.length; i++) {
                    if (el[i].name.substring(el[i].name.lastIndexOf(':')) == id) {
                        el[i].checked = false;
                    }
                }
                radio.checked = true;
            }
        </script>

The error says that:

myaccounts.xhtml:5:52 Expected ) but found ;
                for (var i = 0; i &lt; el.length; i++) {
                                                ^

myaccounts.xhtml:5:57 Expected ; but found )
                for (var i = 0; i &lt; el.length; i++) {
                                                     ^
yrazlik
  • 10,411
  • 33
  • 99
  • 165
  • 2
    http://stackoverflow.com/questions/4338538/javax-servlet-servletexception-error-parsing-page-xhtml-the-content-of-elemen – Andreas Jul 07 '13 at 11:59

2 Answers2

3

Try using CDATA for the XML parser not to read the javascript content, your code would be like this:

<h:head>
    <h:outputStylesheet library="css" name="styles.css"  /> 
    <script>
        // <![CDATA[
        function dataTableSelectOneRadio(radio) {
            var id = radio.name.substring(radio.name.lastIndexOf(':'));
            var el = radio.form.elements;
            for (var i = 0; i < el.length; i++) {
                if (el[i].name.substring(el[i].name.lastIndexOf(':')) == id) {
                    el[i].checked = false;
                }
            }
            radio.checked = true;
        }
        // ]]>
    </script>
</h:head>
morgano
  • 17,210
  • 10
  • 45
  • 56
1

Facelets is a XML based view technology. The characters <, >, and & are special characters in XML. They needs to be replaced by &lt;, &gt;, and &amp; respectively.

for (var i = 0; i &lt; el.length; i++) {
 // your code
}

It is better to wrap the entire JS code in a <![CDATA[ block.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • thanks, i tried it but this time it says: myaccounts.xhtml:5:52 Expected ) but found ; for (var i = 0; i < el.length; i++) { ^ myaccounts.xhtml:5:57 Expected ; but found ) for (var i = 0; i < el.length; i++) { ^ – yrazlik Jul 07 '13 at 12:04