get parent ID in nested script

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

    get parent ID in nested script

    If I have code that looks like this:

    <div id="myID">
    <script type="text/javascript">
    var foo = function() {
    // refer to the id myID here
    };
    </script>
    </div>

    how can the function "foo" reference the parent container DIV's id
    without me having to hard code it?

    Andrew Poulos
  • Bart Van der Donck

    #2
    Re: get parent ID in nested script

    Andrew Poulos wrote:
    <div id="myID">
       <script type="text/javascript">
         var foo = function() {
           // refer to the id myID here
         };
       </script>
    </div>
    >
    how can the function "foo" reference the parent container DIV's
    id without me having to hard code it?
    I don't think this is possible in an OO-model like javascript. The
    code itself is not part of the DOM.

    One workaround is to perform an XMLHttpRequest to the same page,
    filter on '// refer to the id myID here' and fetch the ID of its
    parent tag. You might classify this as hard code.

    --
    Bart

    Comment

    • Stevo

      #3
      Re: get parent ID in nested script

      Andrew Poulos wrote:
      If I have code that looks like this:
      >
      <div id="myID">
      <script type="text/javascript">
      var foo = function() {
      // refer to the id myID here
      };
      </script>
      </div>
      >
      how can the function "foo" reference the parent container DIV's id
      without me having to hard code it?
      Andrew Poulos
      I don't know if it would be suitable in your case, but you could
      document write a marker DIV that you could search for and get it's
      parentNode.

      <div id="myID">
      <script type="text/javascript">
      var foo = function() {
      // refer to the id myID here
      var theDiv = document.getEle mentById("mymar ker").parentNod e;
      };
      document.write( "<div id='mymarker'></div>");
      </script>
      </div>

      Comment

      Working...