Sameh Farahat
Sameh Farahat

Reputation: 1803

Element type "myelement" must be followed by either attribute specifications, ">" or "/>

My script function is defined inside a JSF2 form on a XHTML page and the following part of the code causes a problem:

<script type="text/javascript">
  function myFunction(){
   var checkboxes = document.getElementsByName('mycheckboxes');
   if(checkboxes.length > 0){
     for (var i=0; i<checkboxes.length; i++){
      var checkbox = checkboxes[i];
     }
    }
  }
</script>

When trying to access the page in FireFox 8 it prints the exception:

Element type "checkboxes.length" must be followed by either attribute specifications, ">" or "/>"

what does this error mean?

Another question: is the script is executed before the pages is rendered? Because my checkboxes is loaded in the page during render phase of page (using JSF <ui:repeat>), so my guess is that I must make a condition to execute the code when the variable checkboxes is not null, am I right?

Upvotes: 3

Views: 18456

Answers (3)

DRCB
DRCB

Reputation: 2121

If you write this script directly in the JSF source - you have to escape symbols like >, <, & etc. with &gt;, &amp; ...

Another way is to use CDATA section:

<script language="javascript">//<![CDATA[

/* here your script */

//]]>
</script>

See also:

http://www.w3schools.com/xml/xml_cdata.asp

What characters do I need to escape in XML documents?

Upvotes: 5

PiTheNumber
PiTheNumber

Reputation: 23563

"This error comes from not closing an attribute tag".

http://www.judahfrangipane.com/blog/2006/12/31/element-type-must-be-followed-by-either-attribute-specifications/

Upvotes: 1

Quentin
Quentin

Reputation: 944555

It looks like you are using XHTML and embedding scripts in the page.

XML doesn't have intrinsic CDATA sections, so < means "start of tag" even inside a script element.

If you were using real XHTML then you could represent it as &lt;, but then you couldn't serve the document as text/html.

Instead you can wrap the script content with an explicit CDATA section as described in the specification while also taking care to follow the relevant text/html compatibility guidelines.

You would probably be better off keeping your script in an external file and loading it with <script src="..."></script>.

For that matter, you are likely to be better off using HTML instead of writing XHTML and then jumping through hoops to make it work in browsers that expect HTML.

Upvotes: 6

Related Questions