id attribute of <body> tag

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    id attribute of <body> tag

    I want to dynamically add a hidden <div> </div> to a document when loading my page. It is kind of a generic scratch pad into which I can load messages and data I may want to display to the user. I want to do it this way so I can place it in my base .js file for inclusion in all apps I write without having to reinvent the wheel each time.

    My problem is this; the <body> tag does not seem to pass the "id" value but rather the name of the application. I am using fireFox.

    In this example the obj parameter becomes "appName.ex e". And when I try to use the id in the function addScratchPad(o bj) like this
    var cThisApp = obj.id
    I get an "undefined" error.

    I want to use the id value to insert the scratchPad into the document like this
    document.getEle mentById(cThisA pp).innerHTML = "<div>.... other conten.. </div>

    Here is my code:
    Code:
     <HEAD>
    
    <TITLE> dailyTimeReview </TITLE>
    
      <script type="text/javascript" language="JavaScript"    src="Navigation.js"></script>
    <script language="JavaScript">
    <!--])
     function myStartUp(obj) 
       {   
              addScratchPad(obj);
              aDepts = cMyDepts.split('~');
       }  
    //-->
    </script>
    
    </HEAD>
    
    <body
        id  ="testForm"
        onLoad = "myStartUp(this);"
    >
    function in the .js file

    Code:
    function addScratchPad(obj)
    {
    //this next line fails
    alert(obj.id);
    }
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    that's because "this" points to the window object (FireBug proved useful again).

    to get the id I needed
    Code:
    function addScratchPad(obj)
    {
        alert(obj.document.body.id);
    }

    Comment

    • Claus Mygind
      Contributor
      • Mar 2008
      • 571

      #3
      Hey that works great Dormilich. Thanks for the tip.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        if you're doing quite a lot of Javascript and HTML, FireBug will definitely help you (esp. the script runtime features, and the DOM view)

        Comment

        • Claus Mygind
          Contributor
          • Mar 2008
          • 571

          #5
          Thanks I already use it. I saw what you saw but just did not comprehend it.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            admittedly, fiddling through all the DOM data is a bit difficult.

            Comment

            Working...