469,275 Members | 1,351 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

id attribute of <body> tag

Claus Mygind
571 512MB
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.exe". And when I try to use the id in the function addScratchPad(obj) 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.getElementById(cThisApp).innerHTML = "<div>.... other conten.. </div>

Here is my code:
Expand|Select|Wrap|Line Numbers
  1.  <HEAD>
  2.  
  3. <TITLE> dailyTimeReview </TITLE>
  4.  
  5.   <script type="text/javascript" language="JavaScript"    src="Navigation.js"></script>
  6. <script language="JavaScript">
  7. <!--])
  8.  function myStartUp(obj) 
  9.    {   
  10.           addScratchPad(obj);
  11.           aDepts = cMyDepts.split('~');
  12.    }  
  13. //-->
  14. </script>
  15.  
  16. </HEAD>
  17.  
  18. <body
  19.     id  ="testForm"
  20.     onLoad = "myStartUp(this);"
  21. >
  22.  
function in the .js file

Expand|Select|Wrap|Line Numbers
  1. function addScratchPad(obj)
  2. {
  3. //this next line fails
  4. alert(obj.id);
  5. }
  6.  
Jan 27 '09 #1
5 1695
Dormilich
8,651 Expert Mod 8TB
that's because "this" points to the window object (FireBug proved useful again).

to get the id I needed
Expand|Select|Wrap|Line Numbers
  1. function addScratchPad(obj)
  2. {
  3.     alert(obj.document.body.id);
  4. }
Jan 27 '09 #2
Claus Mygind
571 512MB
Hey that works great Dormilich. Thanks for the tip.
Jan 27 '09 #3
Dormilich
8,651 Expert Mod 8TB
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)
Jan 27 '09 #4
Claus Mygind
571 512MB
Thanks I already use it. I saw what you saw but just did not comprehend it.
Jan 27 '09 #5
Dormilich
8,651 Expert Mod 8TB
admittedly, fiddling through all the DOM data is a bit difficult.
Jan 27 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by francescomoi | last post: by
15 posts views Thread by Frances | last post: by
reply views Thread by Gustavo Arriola | last post: by
1 post views Thread by Gustavo Arriola | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.