SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
-
Oct 29, 2006, 19:50 #1
- 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 } }
Rafael
-
Oct 30, 2006, 01:55 #2
- 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,
-ALLDid I help you?
You can repay me, support one of my projects (no money needed):
JavaScript Wiki, Another Web Forum, Paranormal Site
-
Oct 30, 2006, 08:02 #3
- Join Date
- Dec 2002
- Location
- Alabama, USA
- Posts
- 2,560
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Raffles
Cross-Browser.com, Home of the X Library
-
Oct 30, 2006, 21:11 #4
- 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.
-
Oct 31, 2006, 18:04 #5
- 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.Cross-Browser.com, Home of the X Library
-
Oct 31, 2006, 19:11 #6
- 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?
-
Nov 1, 2006, 10:37 #7
- 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 PrototypeCross-Browser.com, Home of the X Library
Bookmarks