473,406 Members | 2,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

getElementsByTagName select element weirdness

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
5 4523
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: 2obvious | last post by:
This is a pipe dream, I realize, but I'm trying to emulate the functionality of the W3C DOM-supported document.getElementsByTagName method under the very nightmarish Netscape 4. Through some...
4
by: norfleet | last post by:
hi folks, OK, so let's say, for example, I have a bit of HTML that looks like this: <td class="regular1b" valign="top"> <a href="notfound.html"><span class="list5"><b>Lecture...
23
by: Michel Bany | last post by:
I am trying to parse responseXML from an HTTP request. var doc = request.responseXML; var elements = doc.getElementsByTagName("*"); the last statement returns an empty collection when running from...
3
by: hutch | last post by:
Hi, I'm making use of this VERY useful function for a so called 'standards compliant' replacement for target="_blank": function externalLinks() { if (!document.getElementsByTagName) return;...
3
by: Andy | last post by:
Hello, I have the following example XML: <data> <package> <packageid>123</packageid> <package_article> <articleid>article1</articleid> </package_article> </package>
13
by: Oliver Hauger | last post by:
Hello, In my html form I show a select-element and if this element is clicked I fill it per JavaScript/DOM with option-elements. I use the following code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD...
1
by: Max | last post by:
Hello everyone! i would want to know if the getElementsByTagName() function starts to find the elements from documentElement comprising the same documentElement. XML example: <?xml...
4
by: logiczero | last post by:
Here's my code: === var content = document.getElementById('content'); var collection = content.getElementsByTagName('div'); for (var i = 0; i < collection.length; i++) { alert(collection.id);...
3
by: windandwaves | last post by:
does it matter if I write var anchors = document.getElementsByTagName("A"); or var anchors = document.getElementsByTagName("a"); Or is there a better way to catch both <a hrefs and <A...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.