turnitup <same@same> writes:
I need to access the innerHTML property of a div using the dot
notation, eg document.body.div, rather than the GetElementbyID. So far
I have not found the correct way to do it.
If you need to do it without using a method call, and using pure
W3C DOM properties (e.g., no proprietary IE functionality), you
will have to know the location of the div in the document, e.g.,
document.body.firstChild.nextSibling.firstChild
would select a div in
...<body><div><p>Hello</p><div>this is it</div></div>...
However, you don't say why it's important not to use getElementById.
If it is because the element you want doesn't have an id, there
are still easier ways than "dotting your way" in the DOM tree.
If you know it's a div, and you know how many div's are before it
in the document, you can find it as:
document.getElementsByTagName("div")[divIndex]
I would be grateful if anyone could tell me how, or, for future
reference, if there is any function I could use that would tell me the
correct DOM path for an element given an id.
You could, horribly inefficiently, traverse the DOM structure until
you find the node. Or you could use getElementById to find the element
and then backtrace to find the path:
---
function pathOf(element) {
if (typeof element == "string") {
element = document.getElementById(element);
}
var accumulator = [];
while(element != document.body && element != document.head) {
if (element.previousSibling) {
element = element.previousSibling;
accumulator.push("nextSibling");
} else {
element = element.parentNode;
accumulator.push("firstChild");
}
}
accumulator.push((element == document.body)?"body":"head");
accumulator.push("document");
return accumulator.reverse().join(".");
}
---
I think you would get more qualified help if you say what problem
you are really trying to solve :)
/L
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'