By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,605 Members | 2,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,605 IT Pros & Developers. It's quick & easy.

getElementsByTagName select element weirdness

P: n/a
Hi,

On both Opera and Firefox, getElementsByTagName doesn't find anything
apart from <optionelements 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
Aug 2 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Andy Chambers wrote:
On both Opera and Firefox, getElementsByTagName doesn't find anything
apart from <optionelements 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.
<html xmlns="http://www.w3.org/1999/xhtml">
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.dtdlike 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/>
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Aug 2 '08 #2

P: n/a
Thomas 'PointedEars' Lahn wrote:
[...] Suppose your `xref' element was declared in
<http://achambers.me.uk/dtds/ac.dtdlike this:

[...]
And used like this:

<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.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Aug 2 '08 #3

P: n/a
Thomas 'PointedEars' Lahn wrote:
<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
Aug 2 '08 #4

P: n/a
Andy Chambers wrote:
On both Opera and Firefox, getElementsByTagName doesn't find anything
apart from <optionelements 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?

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 3 '08 #5

P: n/a
Andy Chambers wrote:
Thomas 'PointedEars' Lahn wrote:
>><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). :-)
--
Rob
Aug 4 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.