Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Swaping using DOM

    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

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts

    no need for DOM

    code:
    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>

  • #3
    Regular Coder
    Join Date
    Mar 2003
    Posts
    241
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [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 ?

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

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