code to get a parent spans id attribute

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mr_burns

    #1

    code to get a parent spans id attribute

    Hi,
    I am trying to pass a value of a <span> tag's id attribute to a
    function. The following code is a stripped down version of what im
    trying to do:



    <span id="6">Name: <input name="name" type="text"><sp an
    onClick="dosome thing(???);" style="cursor:h and;">Do It!</span></span>



    Where there is a <span> tag with an event handler, onClick, i want to
    replace the ??? with something that will get the value of the parent
    span tag, which in the above case would be '6'. i dont want to simply
    enter 'dosomething(6) ' as the innerHTML of the <span> tag will swap
    between other <span> tags with different attribute values. if the code
    could also be relative if possible too. any help would be great
    because im almost finished my script. cheers

    burnsy
  • Michael Winter

    #2
    Re: code to get a parent spans id attribute

    On 4 Apr 2004 14:33:36 -0700, mr_burns <bissatch@yahoo .co.uk> wrote:
    [color=blue]
    > I am trying to pass a value of a <span> tag's id attribute to a
    > function. The following code is a stripped down version of what im
    > trying to do:
    >
    > <span id="6">Name: <input name="name" type="text"><sp an
    > onClick="dosome thing(???);" style="cursor:h and;">Do It!</span></span>[/color]

    [snip]

    <span ... onclick="dosome thing(this.id)" >

    A quick question, though: what are you going to use the id value for? If
    it's simply to get a reference to the element with document.getEle mentById
    or document.all, save yourself the trouble and pass the element reference
    itself:

    <span ... onclick="dosome thing(this)">

    Good luck,
    Mike

    --
    Michael Winter
    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: code to get a parent spans id attribute

      bissatch@yahoo. co.uk (mr_burns) writes:
      [color=blue]
      > I am trying to pass a value of a <span> tag's id attribute to a
      > function. The following code is a stripped down version of what im
      > trying to do:
      >
      > <span id="6">Name: <input name="name" type="text"><sp an
      > onClick="dosome thing(???);" style="cursor:h and;">Do It!</span></span>[/color]

      First of all, "6" is not a valid value for the id attribute. HTML
      ID's must start with a letter or underscore. I assume you are using
      IE (since you use the IE specific "cursor:han d" and not the valid CSS
      "cursor:pointer "). Don't expect other browsers to be as lenient wrt.
      incorrect HTML.

      (I assume the next to last </span> should be </a>)
      [color=blue]
      > Where there is a <span> tag with an event handler, onClick, i want to
      > replace the ??? with something that will get the value of the parent
      > span tag, which in the above case would be '6'.[/color]

      You can use the W3C DOM to find the parent span tag:
      onclick="dosome thing(this.pare ntNode.id);"

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      Working...