getElementsByTagName select element weirdness

A

Andy Chambers

Hi,

On both Opera and Firefox, getElementsByTagName doesn't find anything
apart from <option> elements inside a select element. Why is this?
Here's a page that demonstrates this behaviour. I'd have thought the
correct behaviour would be to show "count: 1" but instead, you get
"count: 2".

Is this because only <option>s <select>s?

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
</title>
<script type='text/javascript'>
window.onload = function() {
var elems = document.getElementsByTagName("xref");
alert("count: " + elems.length);
};
</script>
<style type='text/css'></style>
</head>
<body>
<div id='banner'>
<h1>
</h1>
</div>
<div id='content'>
<select>
<xref ref="1"/>
</select>
</div>
</body>
</html>


Cheers,
Andy
 
T

Thomas 'PointedEars' Lahn

Andy said:
On both Opera and Firefox, getElementsByTagName doesn't find anything
apart from <option> elements inside a select element. Why is this?

You are trying to use extended XHTML, and your markup is not Valid.[1] The
"Extensible" in XHTML does _not_ mean "anything goes"; if you want to extend
XHTML, you need to declare a namespace for all non-XHTML elements and
attributes and a DTD or a Schema that declares these elements and
attributes. And of course you must comply with the XHTML DTD (that you
missed to declare here), which says in your case (assuming XHTML 1.0
Transitional or Strict):

| <!ELEMENT select (optgroup|option)+> <!-- option selector -->

Meaning that an XHTML `select' element must contain at least one of
`optgroup' or `option' elements.
[...]
Is this because only <option>s <select>s?

Parse error.

This declares the XHTML namespace as the default namespace.
[...]
var elems = document.getElementsByTagName("xref");
[...]
<body>
[...]
<select>
<xref ref="1"/>
</select>
[...]
</body>
</html>

Therefore, you need to use namespace-based accessor methods in an XHTML
document. Suppose your `xref' element was declared in
<http://achambers.me.uk/dtds/ac.dtd> like this:

...
<!ELEMENT select (optgroup|option|xref)+>
...
<!ELEMENT xref EMPTY>
<!ATTLIST xref
ref CDATA #REQUIRED...

And used like this:

<DOCTYPE html PUBLIC "-//AChambers//DTD XHTML 1.0 plus ACMarkup//EN"
"http://achambers.me.uk/dtds/xhtml1-acmarkup.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
xmlns:ac="http://achambers.me.uk/acmarkup"
xml:lang="en">
...
<body>
...
<ac:xref ref="1"/>
...
</body>
</html>

You would access it like this:

var elem = document.getElementsByTagNameNS(
"http://achambers.me.uk/acmarkup", "xref")[0];


PointedEars
___________
[1] <http://validator.w3.org/>
 
A

Andy Chambers

Thomas said:
<DOCTYPE html PUBLIC "-//AChambers//DTD XHTML 1.0 plus ACMarkup//EN"
"http://achambers.me.uk/dtds/xhtml1-acmarkup.dtd">
[...]

Those two URIs must be the same, of course.

So does the browser actually lookup the DTD and validate the document
before loading it? Does it have to be a DTD or could it be something
else that defines your allowable elements (e.g. relaxng or w3c
schema).

Is it possible to extend plain old html in this way or is that the
whole point of xhtml?

Cheers,
Andy
 
M

Martin Honnen

Andy said:
On both Opera and Firefox, getElementsByTagName doesn't find anything
apart from <option> elements inside a select element. Why is this?
Here's a page that demonstrates this behaviour. I'd have thought the
correct behaviour would be to show "count: 1" but instead, you get
"count: 2".

Do you serve the document as text/html or as application/xhtml+xml or
application/xml or text/xml?
Why do you say "doesn't find anything" if count is 2?
 
R

RobG

Andy said:
Thomas said:
<DOCTYPE html PUBLIC "-//AChambers//DTD XHTML 1.0 plus ACMarkup//EN"
"http://achambers.me.uk/dtds/xhtml1-acmarkup.dtd">
[...]
Those two URIs must be the same, of course.

So does the browser actually lookup the DTD and validate the document
before loading it? Does it have to be a DTD or could it be something
else that defines your allowable elements (e.g. relaxng or w3c
schema).

Is it possible to extend plain old html in this way or is that the
whole point of xhtml?


That is getting OT for this news group, though some here likely know the
answer. Try:

news:comp.infosystems.www.authoring.html


But beware, some there have a passionate dislike of XHTML (which is not
a hint to not post, only to be tolerant of some replies you'll get). :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top