473,324 Members | 2,567 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,324 software developers and data experts.

referencing enclosing div tag

Hi all

How do u reference the id (and also now i think about it the name) of
the enclosing div tag of a form element such as a button or div tag
(without specifying its name)? I've tried: this.div.id but it doesnt
work?

Jul 27 '06 #1
2 5449
Hi libsfan01,
I am inferring from your question that you are looking for the first
parent tag that is a div.
Lets see if this gives what you are looking for:
function showRef(formElement){
var parentDiv = findParentDiv(formElement);
if(parentDiv){
alert("found: " + parentDiv.id);
}else{
alert("unable to locate parent div");
}
}

function findParentDiv(elem){
var parent = elem.parentNode;
if(parent && parent.tagName.toUpperCase()!="DIV"){
parent = findParentDiv(parent);
}
return parent;
}

Sample HTML I used for testing:
<form>

<div id="foo">
<span>
<p>
<input type="button" value="test" onclick="showRef(this)" />
</p>
</span>
</div>

</form>

Jul 27 '06 #2
Paul wrote:
Hi libsfan01,
I am inferring from your question that you are looking for the first
parent tag that is a div.
Please reply below a trimmed quote of what you are replying too.

Lets see if this gives what you are looking for:
function showRef(formElement){
var parentDiv = findParentDiv(formElement);
if(parentDiv){
alert("found: " + parentDiv.id);
}else{
alert("unable to locate parent div");
}
}

function findParentDiv(elem){
var parent = elem.parentNode;
if(parent && parent.tagName.toUpperCase()!="DIV"){
A more widely suggested method is to compare with lower case, though in
practice I guess it's a moot point. That will return the first div that
is encountered ascending the DOM tree, which may not be the first one
below the FORM coming down the tree.

The version below will stop once it reaches a form element and will
return the last div that was encountered only if it stopped at a form:

function getDiv(elem){
var div = undefined;
while (elem.parentNode && elem.tagName.toLowerCase()!="form"){
if (elem.tagName.toLowerCase() == "div") div = elem;
elem = elem.parentNode;
}
return elem.tagName
&& elem.tagName.toLowerCase() == "form"
&& div;
}

parent = findParentDiv(parent);
}
return parent;
}
Another method, where 'el' is a reference to a form control, is:

function getDiv(el){
return el.form && el.form.getElementsByTagName('div')[0];
}

Which will return:

- the first div in the DOM tree below the form element if there
is one, whether el is a child of that div or not
- 'null' if there is no div decendent of the form
- 'null' if 'el'isn't a form control (i.e. doesn't have a 'form'
property).

That may suit the OP... or not. e.g.

<script type="text/javascript">
function getDiv(el){
return el.form && el.form.getElementsByTagName('div')[0];
}
</script>

<form action="" style="border: 1px solid red; width: 10em;">
<div style="border: 1px solid green">
<input type="button" value="In a DIV"
onclick="alert(getDiv(this));">
</div>
<p style="border: 1px solid blue">
<input type="button" value="In a P"
onclick="alert(getDiv(this));">
</p>
</form>
<input type="button" value="Not in form"
onclick="alert(getDiv(this));">
--
Rob
Jul 27 '06 #3

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

Similar topics

0
by: Charles Blaquière | last post by:
While noodling around, looking for a good layout for an "events calendar" page, I came upon a problem that has me stymied. Have a look at http://kpuc.org/events/upcoming-2.html . The basic...
4
by: Eric A. Johnson | last post by:
Hi All, I have a class, ConsoleWindow, that is a member of another class, ConsoleLib, like so: class ConsoleLib { public: class ConsoleWindow { public:
5
by: Nik | last post by:
I keep getting the following error 'member names cannot be the same as their enclosing type' with the code below. public class HelpText { private string _textToDisplay; private string...
1
by: | last post by:
in sub class i can't define a constructor it give me error when i want it to... "category member names cannot be the same as their enclosing type" namespace Database { public class Structure...
6
by: Mikey_Doc | last post by:
Hi We are running cms 2002, Framework 1.0 with Visual studio 2002. We have just upgraded to Framework 1.1 and visual studio 2003. All of our database connection strings are stored within the...
9
by: Brett Romero | last post by:
Say I have a library (A.dll) with a method that accepts a collection of a specific type. The type is defined in B.dll. In A.dll, I need to loop through this collection and reference fields of...
7
by: jmp | last post by:
(I hope this isn't considered too far off-topic.) I work as a developer on a browser product found on handheld devices, and I'm trying to specify behavior for the browser to make it "as...
1
by: Veerle | last post by:
Hi, I generate class from xsd files using XSDObjectGen. But the generated code doesn't compile because there are properties with the same name as the class name, which is not allowed in C# :...
12
by: rshepard | last post by:
I'm a bit embarrassed to have to ask for help on this, but I'm not finding the solution in the docs I have here. Data are assembled for writing to a database table. A representative tuple looks...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.