473,320 Members | 1,825 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,320 software developers and data experts.

Accessing innerHTML property of div

Hi,

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.

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.

TIA
Jun 3 '06 #1
5 6102


turnitup wrote:
I need to access the innerHTML property of a div using the dot notation,
eg document.body.div, rather than the GetElementbyID.


I see two dots in
document.getElementById('someId').innerHTML
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 3 '06 #2
turnitup wrote:
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.
As created, there is no such property to be expected in the DOM. But
'need' can only be questioned without its having some justification, and
'dot notation' suggests - eval - and so a better alternative if 'need'
was expalined.
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.


Given sibling relationships there is no single path through the DOM,
unless the DOM in question is trivial. Without a single path there can
be no 'correct' path without external criteria for correctness.

In principle, given any element reference it would be possible to trail
up through - parentNode -, then following its - firstChild - and then -
nextSibling - references to identify the location of the original child,
and build a dot notation property accessor from the parent to the child.
Repeating that up through the document tree until either -
document.body - or - document.documentElement (where supported) were
found, to create an 'absolute' dot notion reference to the original
element. As such a process must start with a reference to that element
it must then be a futile exercise with no worthwhile results as the
original reference could be stored for future reference by more direct
means (in the same way as the resulting dot notiaton property accessor,
in string form, would otherwise be stored for future reference).

Richard.
Jun 3 '06 #3
Thanks for your replies, I have now in fact managed to sort the problem
using the GetElementbyId method, it was a case of building the correct
js string.
Jun 3 '06 #4
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.'
Jun 3 '06 #5
turnitup <same@same> writes:
Thanks for your replies, I have now in fact managed to sort the
problem using the GetElementbyId method, it was a case of building the
correct js string.


As Richard said (IIRC) that sounds like you are using eval.

Eval is generally not recommended, since it is fragile and can mask
otherwise easily found errors (as you seem to have found out).

If programmed property access is necessary, using square bracket
notation almost always solves it safer and even more efficiently
than using eval, e.g.,
var myPropName = "someString";
//...
object[myPropName]; //no eval needed

Good luck
/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.'
Jun 3 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris New | last post by:
G'Day All I am having trouble dynamically assigning a value to a table's cell from one frame to another frame. I've created a website that is primarily viewed in a frameset consisting of 2...
4
by: RobG | last post by:
I know you aren't supposed to use innerHTML to mess with table structure, but it seems you can't use it just after a table without damaging the containing element. I added a table to a div using...
0
by: msnews.microsoft.com | last post by:
Hi, IN ASP.NET PAGE I HAVE DEFINED THE DIV TAG AS FOLLOWS:I want to access the value in the code behind(c#) . <div align="left" autopostback = true class="clsTitle"...
5
by: Dan Nash | last post by:
Hi all, I've got a page with a user control on, added via VS. I'm trying to get to a property of the user control (or more precisely, a public var). Here's the code at the top of my aspx...
0
by: sonu | last post by:
I have following client side code which i have used in my asp.net project SummaryFeatured Resources from the IBM Business Values Solution Center WHITEPAPER : CRM Done Right Improve the...
9
by: Hallvard B Furuseth | last post by:
Why does the FAQ (Q 4.15) recommend innerHTML when so many here say one should use createElement(), replaceChild() etc? Also, why does the "Alternative DynWrite function" at...
14
by: Aaron Gray | last post by:
Hi, I want to access the properties of an IFrame but seem unable to get access to the IFrames document body. <html> <body> <iframe src="test.html" id="IFrame"></iframe> </body>
9
by: martymix | last post by:
simple question: I have a simple <dt>test text</dt> I get the innerHTML of that dt, and I try and append some text to it like so: dt = document.getElementsByTagName('dt') var text =...
6
by: PaPa | last post by:
I'm not sure this is a javascript issue or an HTML issue. I notice that when I extract the contents of a div using the innerHTML property (?), that I wind up with a literal variable (?) which...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.