SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Mar 2004
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    switch layer visibility

    I've successfully integrated this script http://www.dyn-web.com/dhtml/show-hide/show-hd.html to show and hide images on a page.

    Here is what it looks like:
    http://www.truedesign.co.uk/true/3mobile.html

    The 1 2 3 navigation is CSS based and maybe not appropriate, because I want the highlighted numbers to stick depending on what image is shown.

    Unfortunately I don't know JS, so I can't code it myself. Is there something a kind person can point me to that I can implement? Many thanks.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Instead of highlighting the numbers based on their id name, you should create a class that can be shifted to the appropriate number instead.

    Code CSS:
    .selected {
      font-weight: bold;
    }

    Then you can set whichever element to have (or not have) that class.

    Code JavaScript:
    var el;
    el = document.getElementById('one');
    el.setAttribute('class', '');
    el = document.getElementById('two');
    el.setAttribute('class', 'selected');
    el = document.getElementById('three');
    el.setAttribute('class', '');

    That's all you need.

    What follows below is an example of removing unnecessary duplication from the function.

    Code JavaScript:
    function setClass(el, value) {
      el.setAttribute('class', value);
    }
     
    setClass(document.getElementById('one'), '');
    setClass(document.getElementById('two'), 'selected');
    setClass(document.getElementById('three'), '');

    and further removal of duplication

    Code JavaScript:
    function $(id) {
      return document.getElementById(id);
    }
    function setClass(el, value) {
      el.setAttribute('class', value);
    }
     
    setClass($('one'), '');
    setClass($('two'), 'selected');
    setClass($('three'), '');

  3. #3
    SitePoint Addict
    Join Date
    Mar 2004
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks for your kind reply. I guess that's where it stops for me, because of lack of JS knowledge (e.g. can't see why "selected" is there after "two", as it seems that two is always the selected style...).

    I better go and learn some JS.


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
  •