...

View Full Version : Swaping using DOM



baz7621
03-10-2004, 08:46 AM
Hiya

If i had a table with 1 row and 2 cells. Each cell had an image in it and when i click on either image they would swap. So Image 1 would be in cell 2 and image 2 would be in cell 1.

Someone told me about the swapping nodes method in DOM but i am new to javascript so any help at all would be nice

glenngv
03-10-2004, 09:37 AM
code:


function swap(imgName1, imgName2){
var temp = document.images[imgName1].src;
document.images[imgName1].src = document.images[imgName2].src;
document.images[imgName2].src = temp;
return false;
}

html:
<td><a href="#" onclick="return swap('img1','img2')"><img src="image1.gif" name="img1"></a></td>
<td><a href="#" onclick="return swap('img2','img1')"><img src="image2.gif" name="img2"></a></td>

Caffeine
03-12-2004, 08:06 AM
[a bit off-topic, but still related :)]

Is there any reasons why one should use
document.images['imgID'].src
instead of
document.getElementById('imgID').src
??

Would document.images be faster since it does not step through the whole document but just the array of images ?

liorean
03-12-2004, 08:59 AM
Originally posted by Caffeine
Would document.images be faster since it does not step through the whole document but just the array of images ? It may be, depending on how browsers handle it. The DOM is not exactly defining mechanisms. In current browsers they simply perform the same actions, though. One argument for using document.images would be the wider support for that.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum