An Attribute of a Parent Element?

Discussion in 'Javascript' started by pbd22, Oct 17, 2007.

  1. pbd22

    pbd22 Guest

    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?
    pbd22, Oct 17, 2007
    #1
    1. Advertising

  2. 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.
    The Natural Philosopher, Oct 17, 2007
    #2
    1. Advertising

  3. 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
    Thomas 'PointedEars' Lahn, Oct 17, 2007
    #3
  4. 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
    Thomas 'PointedEars' Lahn, Oct 17, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MSNews
    Replies:
    1
    Views:
    2,711
    Alvin Bruney [MVP]
    Apr 22, 2004
  2. Bostonasian
    Replies:
    1
    Views:
    810
    Joris Gillis
    Sep 18, 2005
  3. Replies:
    4
    Views:
    2,515
  4. Replies:
    1
    Views:
    502
    Pavel Lepin
    Feb 19, 2008
  5. Bitswapper
    Replies:
    5
    Views:
    120
    Prasad, Ramit
    Aug 27, 2013
Loading...

Share This Page