473,386 Members | 1,654 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,386 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 6104


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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.