SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Who is Mr Blonde? Mr Blonde's Avatar
    Join Date
    Sep 2006
    Location
    Gold Coast, QLD
    Posts
    359
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    swap function - can swap more than 1 id?

    hi,
    i hav been using a swap function for 2 items that when a link is clicked, the divs that are displayed are swapped...
    HTML Code:
    <script type="text/javascript">
    function swap(show,hide) {
     if (document.getElementById)
     {
      var style2 = document.getElementById(show).style;
      style2.display = "block";
      var style3 = document.getElementById(hide).style;
      style3.display = "none";
     }
    }
    </script>
    HTML Code:
    <p><a onclick="swap('content1','content2');return false;" href="#">1</a></p>
    <p><a onclick="swap('content2','content1');return false;" href="#">2</a></p>
    i now have to add another 2 ID's to this to show 1 and hide the other 3... is there a way i can add the other 2 to be hidden in this or should i use another function to create 'previous', 'next' buttons.

    thanks
    Heavens Rejects : Online Clothing Store
    Alternative Clothing : Mr Blonde
    Front end development : By the Webfella

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Split your current function into 2. One for showing, and one for hiding.
    Code:
    <div id="bertil">tt</div>
    <div id="anna">a</div>
    
    <script>
    function show() {
        for(var i=0; i<arguments.length; i++) {
            document.getElementById(arguments[i]).style.display = "block";
        }
    }
    
    function hide() {
        for(var i=0; i<arguments.length; i++) {
            document.getElementById(arguments[i]).style.display = "none";
        }
    }
    
    // Just pass in the ids of the elements you wanna hide, same goes for show
    hide("bertil", "anna");
    </script>

  3. #3
    Who is Mr Blonde? Mr Blonde's Avatar
    Join Date
    Sep 2006
    Location
    Gold Coast, QLD
    Posts
    359
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    awesome thanks man, not too familiar with scripting so just couldn't see it.

    cheers
    Heavens Rejects : Online Clothing Store
    Alternative Clothing : Mr Blonde
    Front end development : By the Webfella

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I recommend you "JavaScript Bible, 5th Edition" if you would like to dig deeper into JavaScript.

  5. #5
    Who is Mr Blonde? Mr Blonde's Avatar
    Join Date
    Sep 2006
    Location
    Gold Coast, QLD
    Posts
    359
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah man in the future for sure.... just have to conquer a few other languages 1st
    Heavens Rejects : Online Clothing Store
    Alternative Clothing : Mr Blonde
    Front end development : By the Webfella


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
  •