Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > An Attribute of a Parent Element?

Reply
Thread Tools

An Attribute of a Parent Element?

 
 
pbd22
Guest
Posts: n/a
 
      10-17-2007
Hi.

Could some kind sould tell me what the javascript would be to get a
given attribute of the parent "table" element of a given element
inside that table.

So,

<table width="54">
[SNIP - LOTS OF HTML HERE]
<a href="#" onclick="Foo();">get the width attribute's value</a>
</table>

When I click on the anchor tag, it returns the number "54".

How do I do this?

 
Reply With Quote
 
 
 
 
The Natural Philosopher
Guest
Posts: n/a
 
      10-17-2007
pbd22 wrote:
> Hi.
>
> Could some kind sould tell me what the javascript would be to get a
> given attribute of the parent "table" element of a given element
> inside that table.
>
> So,
>
> <table width="54">
> [SNIP - LOTS OF HTML HERE]
> <a href="#" onclick="Foo();">get the width attribute's value</a>
> </table>
>
> When I click on the anchor tag, it returns the number "54".
>
> How do I do this?
>

Give the table an ID and getElementById on it.
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      10-17-2007
pbd22 wrote:
> <table width="54">


The `width' attribute is presentational. Use CSS instead.

> [SNIP - LOTS OF HTML HERE]
> <a href="#" onclick="Foo();">get the width attribute's value</a>


That element should be generated by client-side scripting:

<script type="text/javascript">
document.write('<a ...>...<\/a>');
</script>

Foo() would not be a constructor or a factory, and so should be foo().

> </table>
>
> When I click on the anchor tag, it returns the number "54".
>
> How do I do this?


You don't, as you cannot click on a tag if not in your source code editor.

Provided that you mean a click on the `a' element there, you have to obtain
the ancestor element object reference through iteration:

function getNextAncestor(o, sElemType)
{
if (o)
{
var rxElemType;

if (typeof sElemType == "string")
{
rxElemType = new RegExp(sElemType.toLowerCase(), "i");
}

if (rxElemType)
{
while ((o = o.parentNode))
{
if (rxElemType.test(o.tagName))
{
return o;
}
}
}
}

return null;
}

function getAncestorTableWidth(o)
{
return (getNextAncestor(o, "table") || {width: -1}).width;
}

<table width="54">
...
<tr>
<td>...<a
href="#"
onclick="window.alert(getAncestorTableWidth(this)) ; return false"
>get the width attribute's value</a>...</td>

</tr>
...
</table>


HTH

PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      10-17-2007
Thomas 'PointedEars' Lahn wrote:
> rxElemType = new RegExp(sElemType.toLowerCase(), "i");


`.toLowerCase()' can be safely omitted here.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
XSLT: Making attribute to parent attribute Bostonasian XML 1 09-18-2005 07:30 AM
Page load of the parent page called twice when a modal dialog is opened from a button click of the user control on the parent page Samy ASP .Net 2 08-15-2005 03:30 PM
Parent - Child back to parent javascript thingybob mark ASP .Net 1 03-10-2005 02:20 PM
What does sender.Parent.Parent.Cells() reference? Frustrating... Roy ASP .Net 2 02-11-2005 09:00 PM



Advertisments