SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Swapping ID, DOM not being updated?

    Hi, I am using a script to change the id of an element in order to change its appearance and position. I am doing this:
    Code:
    window.onload = function() {
      var d = document.getElementById('<?php echo $where; ?>'); // $where can be left or right
      var f = document.getElementById('move');
        f.onclick = function () {
          if (d.getAttribute('id') === 'left') {
            d.setAttribute('id', 'right');
          }
          else if (d.getAttribute('id') === 'right') {
            d.setAttribute('id','left');
          }
        }
    
        if (d.getAttribute('id') === 'left') {
          // do things
        }
          
        if (d.getAttribute('id') === 'right') {
          // do other things
        }
      }
    Upon clicking, the CSS associated with the new ID is applied, so that works. The problem is that the things in the last two functions are not being applied. It seems like I would have to somehow poke the JS to tell it that the id has been changed and to apply those things. How do I do this? Cheers,

    Rafael

  2. #2
    SitePoint Addict ALL's Avatar
    Join Date
    Oct 2005
    Location
    South Dakota
    Posts
    215
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    well look closer at the code...

    it will run thought the last two if's and execute them only when toe window.onload executes which should only be once unless refreshed...

    it will not run those two because onload is only ran once...

    also... i seggest making the d and f variables globals by putting them outside the onload function.

    if you can put this up online and post a link so we can see it to better serve you,
    -ALL
    Did I help you?
    You can repay me, support one of my projects (no money needed):
    JavaScript Wiki, Another Web Forum, Paranormal Site

  3. #3
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles
    I am using a script to change the id of an element in order to change its appearance and position.
    Suggestion: Don't change the ID - change the CLASS.

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Right, thanks, I've fixed it. Using it here: http://raffles.awardspace.com/G/ just for fun and to learn some more JS. Unfortunately it's not working seamlessly in IE (when putting it back on the left), can't figure out why.

    Mike, why the class and not the id? I chose that because getElementById makes it so easy, whereas there's no such (default) thing as getElementsByClassName.

    EDIT: Just realised I can still use getElementById and then use setAttribute('class')... hopefully this is the best thing to do.
    Last edited by Raffles; Oct 30, 2006 at 21:45.

  5. #5
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Raffles,

    It looks like you have it figured out

    I just use IDs for uniquely identifying elements. CLASS was intended for assignment of styles.

    Here's my xGetElementsByClassName, and here's two more handy ones: xAddClass and xRemoveClass.

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That is very cool, nice work. I had never heard of X before. How is it different to prototype, which seems to have exploded in popularity recently?

  7. #7
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

    To be honest, I haven't looked under the hood of Prototype


Bookmarks

Posting Permissions

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