Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
cartimus

Retrieve An Unknown Id Value

Recommended Posts

What is the syntax for specifying the ID attribute of HTML elements - soemthing like 'document.body.id ?
If I wanted to retrieve the 'id=' value of the body element (assuming it is unknown), what statement would handle that?

For example, if each of my pages had a unique ID for the BODY tag, so I can write a common function that does different things depending on which page the user loaded, how would I do this?
 

 

Share this post


Link to post
Share on other sites

What is the syntax for specifying the ID attribute of HTML elements - soemthing like 'document.body.id ?

Yes, that's it. You can both read and write the ID attribute of the body element by using document.body.id.

 

You might, for example, write a function in your JS that does something like the following:

switch (document.body.id) {
  
  case 'home':
    
    // The home page. Do something.
    
    break;
    
  case 'about':
    
    // The About Us page. Do something.
    
    break;
    
  case 'site_map':
    
    // The Site Map page. Do something.
    
    break;
    
  default:
    
    // Have a default action.
  
}
  • Upvote 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...