473,662 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to use JS to get an outer div id?

Hi,

I have a block of HTML of this form:

<div id="outer0" class="outerDiv ">
<div class="innerDiv ">
<table height="100%" id="table0">
<tr style="backgrou nd-color:#e0e0e0; height:15px;">< td
align="right">< a href="javascrip t:closeMe('/* Javascript goes here
*/');">Click Me</a></td></tr>
<!-- There may be more table rows -->
</table>
</div>
</div>

In the place where you see "/* Javascript goes here */", I would like
to put an expression that evaluates to the id of the outer most DIV,
so in this case, it would evaluate to "outer0". Does anyone know a
cross-browser JS expression that will do this?

Thanks, - Dave

Jan 31 '07 #1
10 2638
<la***********@ zipmail.comwrot e in message
news:11******** *************@s 48g2000cws.goog legroups.com...
Hi,

I have a block of HTML of this form:

<div id="outer0" class="outerDiv ">
<div class="innerDiv ">
<table height="100%" id="table0">
<tr style="backgrou nd-color:#e0e0e0; height:15px;">< td
align="right">< a href="javascrip t:closeMe('/* Javascript goes here
*/');">Click Me</a></td></tr>
<!-- There may be more table rows -->
</table>
</div>
</div>

In the place where you see "/* Javascript goes here */", I would like
to put an expression that evaluates to the id of the outer most DIV,
so in this case, it would evaluate to "outer0". Does anyone know a
cross-browser JS expression that will do this?
document.getEle mentsByTagName( 'div')[0]

That should do it.

-Lost
Jan 31 '07 #2
-Lost said the following on 1/30/2007 7:22 PM:
<la***********@ zipmail.comwrot e in message
news:11******** *************@s 48g2000cws.goog legroups.com...
>Hi,

I have a block of HTML of this form:

<div id="outer0" class="outerDiv ">
<div class="innerDiv ">
<table height="100%" id="table0">
<tr style="backgrou nd-color:#e0e0e0; height:15px;">< td
align="right"> <a href="javascrip t:closeMe('/* Javascript goes here
*/');">Click Me</a></td></tr>
<!-- There may be more table rows -->
</table>
</div>
</div>

In the place where you see "/* Javascript goes here */", I would like
to put an expression that evaluates to the id of the outer most DIV,
so in this case, it would evaluate to "outer0". Does anyone know a
cross-browser JS expression that will do this?

document.getEle mentsByTagName( 'div')[0]

That should do it.
It won't. Consider:

<div id="outerDivYou Get"></div>
<div id="outerDivWan ted">
<div id="innerDiv">D o you still think that
document.getEle mentsByTagName( 'div')[0]
still refers to the outer most div element
of this element? Hint: It doesn't.
</div>
--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 31 '07 #3
On Jan 31, 10:22 am, "-Lost" <spam_ninjaREMO V...@REMOVEMEco mcast.net>
wrote:
<laredotorn...@ zipmail.comwrot e in message

news:11******** *************@s 48g2000cws.goog legroups.com...
Hi,
I have a block of HTML of this form:
<div id="outer0" class="outerDiv ">
<div class="innerDiv ">
<table height="100%" id="table0">
<tr style="backgrou nd-color:#e0e0e0; height:15px;">< td
align="right">< a href="javascrip t:closeMe('/* Javascript goes here
*/');">Click Me</a></td></tr>
<!-- There may be more table rows -->
</table>
</div>
</div>
In the place where you see "/* Javascript goes here */", I would like
to put an expression that evaluates to the id of the outer most DIV,
so in this case, it would evaluate to "outer0". Does anyone know a
cross-browser JS expression that will do this?

document.getEle mentsByTagName( 'div')[0]

That should do it.
Only if the outermost div is the first in the document. Consider:

<div... </div>
<div>
<div>What is my outermost div?</div>
</div>
The following function climbs up the DOM and returns a reference to
the last div encountered. If there is no enclosing div, it returns
undefined.

function getOutermostDiv (el) {
var outermostDiv;
while (el.parentNode && (el = el.parentNode)) {
if (el.tagName && el.tagName.toLo werCase() == 'div'){
outermostDiv = el;
}
}
return outermostDiv;
}
And a test:

<div id="blah">
<div id="blah00">
<button onclick="
var x = getOutermostDiv (this);
if (x) {
(x.id)? alert(x.id):ale rt('no id');
} else {
alert('no outer div');
}
">Show outer-most DIV id</button>
</div>
</div>

--
Rob

Jan 31 '07 #4
On Jan 30, 6:10 pm, "laredotorn...@ zipmail.com"
<laredotorn...@ zipmail.comwrot e:
Hi,

I have a block of HTML of this form:

<div id="outer0" class="outerDiv ">
<div class="innerDiv ">
<table height="100%" id="table0">
<tr style="backgrou nd-color:#e0e0e0; height:15px;">< td
align="right">< a href="javascrip t:closeMe('/* Javascript goes here
*/');">Click Me</a></td></tr>
<!-- There may be more table rows -->
</table>
</div>
</div>

In the place where you see "/* Javascript goes here */", I would like
to put an expression that evaluates to the id of the outer most DIV,
so in this case, it would evaluate to "outer0". Does anyone know a
cross-browser JS expression that will do this?

Thanks, - Dave
Try parentNode and a while loop.

Jan 31 '07 #5
"Randy Webb" <Hi************ @aol.comwrote in message
news:Pt******** ************@te lcove.net...
-Lost said the following on 1/30/2007 7:22 PM:
><la*********** @zipmail.comwro te in message
news:11******* **************@ s48g2000cws.goo glegroups.com.. .
>>Hi,

I have a block of HTML of this form:

<div id="outer0" class="outerDiv ">
<div class="innerDiv ">
<table height="100%" id="table0">
<tr style="backgrou nd-color:#e0e0e0; height:15px;">< td
align="right" ><a href="javascrip t:closeMe('/* Javascript goes here
*/');">Click Me</a></td></tr>
<!-- There may be more table rows -->
</table>
</div>
</div>

In the place where you see "/* Javascript goes here */", I would like
to put an expression that evaluates to the id of the outer most DIV,
so in this case, it would evaluate to "outer0". Does anyone know a
cross-browser JS expression that will do this?

document.getEl ementsByTagName ('div')[0]

That should do it.

It won't. Consider:

<div id="outerDivYou Get"></div>
<div id="outerDivWan ted">
<div id="innerDiv">D o you still think that
document.getEle mentsByTagName( 'div')[0]
still refers to the outer most div element
of this element? Hint: It doesn't.
</div>
But I was responding to the original poster's *original* markup. In *that* sense, I was
correct, right?

I see RobG provided a function that would always reach the outermost DIV. I guess I
should have thought of something like that (not sure that I could have to be honest). But
based on what the original post was, I thought my answer sufficed. Not if the original
poster's markup was changed.

Am I way off base?

Thanks.

-Lost
Jan 31 '07 #6
-Lost said the following on 1/31/2007 1:17 AM:
"Randy Webb" <Hi************ @aol.comwrote in message
news:Pt******** ************@te lcove.net...
>-Lost said the following on 1/30/2007 7:22 PM:
>><la********** *@zipmail.comwr ote in message
news:11****** *************** @s48g2000cws.go oglegroups.com. ..
Hi,

I have a block of HTML of this form:

<div id="outer0" class="outerDiv ">
<div class="innerDiv ">
<table height="100%" id="table0">
<tr style="backgrou nd-color:#e0e0e0; height:15px;">< td
align="right "><a href="javascrip t:closeMe('/* Javascript goes here
*/');">Click Me</a></td></tr>
<!-- There may be more table rows -->
</table>
</div>
</div>

In the place where you see "/* Javascript goes here */", I would like
to put an expression that evaluates to the id of the outer most DIV,
so in this case, it would evaluate to "outer0". Does anyone know a
cross-browser JS expression that will do this?
document.getE lementsByTagNam e('div')[0]

That should do it.
It won't. Consider:

<div id="outerDivYou Get"></div>
<div id="outerDivWan ted">
<div id="innerDiv">D o you still think that
document.getEl ementsByTagName ('div')[0]
still refers to the outer most div element
of this element? Hint: It doesn't.
</div>

But I was responding to the original poster's *original* markup. In *that* sense, I was
correct, right?
Given the HTML markup, yes, your code gives the containing div. But only
if that is the markup with no other code in the page. Life isn't always
that simple though :-)
I see RobG provided a function that would always reach the outermost DIV. I guess I
should have thought of something like that (not sure that I could have to be honest). But
based on what the original post was, I thought my answer sufficed. Not if the original
poster's markup was changed.

Am I way off base?
Nah, after 10 years or so around here you start QC'ing any posted code
to see if it can be broken. If it can't, then it is a pretty good bet
that you have solid code that can be reused elsewhere instead of trying
to come up with a one-off solution. One-off solutions have a place though.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 31 '07 #7
"Randy Webb" <Hi************ @aol.comwrote in message
news:Nc******** ************@te lcove.net...
-Lost said the following on 1/31/2007 1:17 AM:
>"Randy Webb" <Hi************ @aol.comwrote in message
news:Pt******* *************@t elcove.net...
>>-Lost said the following on 1/30/2007 7:22 PM:
<la********* **@zipmail.comw rote in message
news:11***** *************** *@s48g2000cws.g ooglegroups.com ...
Hi,
>
I have a block of HTML of this form:
>
<div id="outer0" class="outerDiv ">
<div class="innerDiv ">
<table height="100%" id="table0">
<tr style="backgrou nd-color:#e0e0e0; height:15px;">< td
align="righ t"><a href="javascrip t:closeMe('/* Javascript goes here
*/');">Click Me</a></td></tr>
<!-- There may be more table rows -->
</table>
</div>
</div>
>
In the place where you see "/* Javascript goes here */", I would like
to put an expression that evaluates to the id of the outer most DIV,
so in this case, it would evaluate to "outer0". Does anyone know a
cross-browser JS expression that will do this?
document.get ElementsByTagNa me('div')[0]

That should do it.
It won't. Consider:

<div id="outerDivYou Get"></div>
<div id="outerDivWan ted">
<div id="innerDiv">D o you still think that
document.getE lementsByTagNam e('div')[0]
still refers to the outer most div element
of this element? Hint: It doesn't.
</div>

But I was responding to the original poster's *original* markup. In *that* sense, I
was correct, right?

Given the HTML markup, yes, your code gives the containing div. But only if that is the
markup with no other code in the page. Life isn't always that simple though :-)
Too true... too... true.
>I see RobG provided a function that would always reach the outermost DIV. I guess I
should have thought of something like that (not sure that I could have to be honest).
But based on what the original post was, I thought my answer sufficed. Not if the
original poster's markup was changed.

Am I way off base?

Nah, after 10 years or so around here you start QC'ing any posted code to see if it can
be broken. If it can't, then it is a pretty good bet that you have solid code that can
be reused elsewhere instead of trying to come up with a one-off solution. One-off
solutions have a place though.
Definitely makes sense! I think maybe I will refrain from responding as much unless I
have a reusable (module) piece of code.

I remember reading once (I think on an Algebra newsgroup) that "to learn" you should "post
often, and try to answer as much as you can." That just does not hold true for this
group. I feel silly every time I provide an ad-hoc solution.

I even sit here thinking, "Man, I think this is a decent solution." When I see that it is
not I just smack myself on the forehead wondering "Why did I not think of that?"

OK, enough boo-hoo-ing. : )

-Lost
Jan 31 '07 #8
-Lost said the following on 1/31/2007 1:25 PM:
"Randy Webb" <Hi************ @aol.comwrote in message
news:Nc******** ************@te lcove.net...
<snip>
>Nah, after 10 years or so around here you start QC'ing any posted code to see if it can
be broken. If it can't, then it is a pretty good bet that you have solid code that can
be reused elsewhere instead of trying to come up with a one-off solution. One-off
solutions have a place though.

Definitely makes sense! I think maybe I will refrain from responding as much unless I
have a reusable (module) piece of code.
Then you won't learn. You can only learn just so much before you have to
start posting code or you won't ever see what is wrong with your approach.
I remember reading once (I think on an Algebra newsgroup) that "to learn" you should "post
often, and try to answer as much as you can." That just does not hold true for this
group. I feel silly every time I provide an ad-hoc solution.
Don't. We all did. I did, Richard did, RobG did, we all did. Post a
potential solution. Just be prepared to have your head slammed into a
wall if you didn't think it out.
I even sit here thinking, "Man, I think this is a decent solution." When I see that it is
not I just smack myself on the forehead wondering "Why did I not think of that?"
That's when you make a mental note to remember it next time. It's the
surest way to learning is trying. 'Tis better to try and fail than to
have never tried.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 31 '07 #9
"Randy Webb" <Hi************ @aol.comwrote in message
news:uM******** ************@te lcove.net...
-Lost said the following on 1/31/2007 1:25 PM:
>"Randy Webb" <Hi************ @aol.comwrote in message
news:Nc******* *************@t elcove.net...

<snip>
>>Nah, after 10 years or so around here you start QC'ing any posted code to see if it
can be broken. If it can't, then it is a pretty good bet that you have solid code that
can be reused elsewhere instead of trying to come up with a one-off solution. One-off
solutions have a place though.

Definitely makes sense! I think maybe I will refrain from responding as much unless I
have a reusable (module) piece of code.

Then you won't learn. You can only learn just so much before you have to start posting
code or you won't ever see what is wrong with your approach.
OK. I thought perhaps I might be getting on the nerves of the regulars (including
yourself). Thanks for the advice.
>I remember reading once (I think on an Algebra newsgroup) that "to learn" you should
"post often, and try to answer as much as you can." That just does not hold true for
this group. I feel silly every time I provide an ad-hoc solution.

Don't. We all did. I did, Richard did, RobG did, we all did. Post a potential solution.
Just be prepared to have your head slammed into a wall if you didn't think it out.
Definitely. I can handle criticism. I have a neurological disorder though, so I hope you
guys will not slam it too hard against the wall. I need all the thinking power I have
left.
>I even sit here thinking, "Man, I think this is a decent solution." When I see that it
is not I just smack myself on the forehead wondering "Why did I not think of that?"

That's when you make a mental note to remember it next time. It's the surest way to
learning is trying. 'Tis better to try and fail than to have never tried.
Oh yeah, definitely. I have so many posts marked "watch" in my newsreader it is pathetic.
I also take the code posted and insert it in a working HTML document with notes as to who
wrote it. Like I did with this thread and RobG's code.

Be well.

-Lost
Feb 1 '07 #10

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

Similar topics

2
18109
by: Martin | last post by:
I am now working on SQL Server 2000 having had previous experience on a different database. Both of the OUTER JOIN syntaxes is different from what I am used to and I am finding it slightly confusing. For example, given two tables : wipm_tbl_mi wipm_tbl_wi (which may not have data in it for a specific record that exists in the first table.)
8
4968
by: Matt | last post by:
Hello I have to tables ar and arb, ar holds articles and a swedish description, arb holds descriptions in other languages. I want to retreive all articles that match a criteria from ar and also display their corresponding entries in arb, but if there is NO entry in arb I still want it to show up as NULL or something, so that I can get the attention that there IS no language associated with that article.
4
4861
by: thilbert | last post by:
All, I have a perplexing problem that I hope someone can help me with. I have the following table struct: Permission ----------------- PermissionId Permission
3
10046
by: Dam | last post by:
Using SqlServer : Query 1 : SELECT def.lID as IdDefinition, TDC_AUneValeur.VALEURDERETOUR as ValeurDeRetour FROM serveur.Data_tblDEFINITIONTABLEDECODES def, serveur.Data_tblTABLEDECODEAUNEVALEUR TDC_AUneValeur where def.TYPEDETABLEDECODES = 4
3
19470
by: Martin | last post by:
Hello everybody, I have the following question. As a join clause on Oracle we use " table1.field1 = table2.field1 (+) " On SQL Server we use " table1.field1 *= table2.field1 " Does DB2 have the same type of operator, without using the OUTER JOIN syntax ?
14
5699
by: mike | last post by:
I'm using postgresl 7.3.2 and have a query that executes very slowly. There are 2 tables: Item and LogEvent. ItemID (an int4) is the primary key of Item, and is also a field in LogEvent. Some ItemIDs in LogEvent do not correspond to ItemIDs in Item, and periodically we need to purge the non-matching ItemIDs from LogEvent. The query is: delete from LogEvent where EventType != 'i' and ItemID in
2
2935
by: dobest03 | last post by:
Hi. Are there any way to access the integer member 'a' of outer structure from inner structure's member function func_inner()? See below the structure... Thanks. struct outer {
5
2425
by: Martijn Mulder | last post by:
A construction like this: class Outer { class Inner:Outer { } } compiles without problem but does it introduce infinity?
9
4681
by: Matthias Buelow | last post by:
Hi folks, I've got something like: class Outer { int f(); friend class Inner; class Inner { int g() {
3
2076
by: ChrisW | last post by:
Hiya, So I have a class that creates threads within it. These threads are a class underneath the parent class. I want to access values in the parent class from the threads while they run. Yet this gives me an error: cannot access a non-static member of outer type 'Parent.Class' via nested type 'Parent.Class.Thread'. So how do I access values in the parent class?
0
8432
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8344
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
7367
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5654
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.