www.webdeveloper.com
Results 1 to 3 of 3

Thread: getElementById question - Please help

  1. #1
    Join Date
    Nov 2006
    Posts
    5

    getElementById question - Please help

    Hi Everybody,

    I'd be grateful if you could help me out. I need to change the CSS by changing the id when a link is clicked.
    The id is in the body tag and by default it is named 'first'. i.e
    [ICODE]<body id="first">[/ICODE].
    If I click on the link named 'Change id to Second', this should change to [ICODE]<body id="Second">[/ICODE].
    If I further click on the link named "Change id to Second" this should change to
    [ICODE]<body id="Third">[/ICODE].

    My code below works (I have stripped this down to make sense), but only when I click one of the links for the first time. If I am to click the second/ third link, nothing happens:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function changeFirst() {
                     document.getElementById('blanky').id  = 'first';
                  	 document.getElementById('second').id  = 'first';
    		 document.getElementById('third').id   = 'first';
                 }
    function changeSecond() {
                     document.getElementById('blanky').id = 'second';
                  	 document.getElementById('first').id  = 'second';
    		 document.getElementById('third').id  = 'second';
                 }
    function changeThird() {
                     document.getElementById('blanky').id  = 'third';
                  	 document.getElementById('first').id   = 'third';
    		 document.getElementById('second').id  = 'third';
                 }
    </script>
    </head>
    <body id=first><!-- I need to change this id -->
    <p>Hello, the id above should change when the following links are clicked</p>
    <a  onclick="changeFirst()">Change id to First</a>
    <a  onclick="changeSecond()">Change id to Second</a>
    <a  onclick="changeThird()">Change id to Third</a>
    </body>
    </html>
    Any help will be greatly appreciated.

    Cheers

  2. #2
    Join Date
    Dec 2005
    Posts
    2,984
    HTML Code:
    <a href="#" onclick="changebodyid('theId');">your link</a>
    Code:
    function changebodyid(theid) {
           document.body.id = theid;
    }

  3. #3
    Join Date
    Nov 2006
    Posts
    5

    Thank you

    You Sir, are a genius!
    Thank you so very much!!!!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles