www.webdeveloper.com
Results 1 to 3 of 3

Thread: counting id attributes

  1. #1
    Join Date
    Feb 2005
    Location
    The dark place
    Posts
    91

    counting id attributes

    Hi

    Javascript newbie here - I know my way around a bit of serverside and SQL stuff but client side still eludes me slightly.

    On a page, I have several div tags with incrementally numbered id attributes:
    Code:
    <div id="content[1]">blah blah</div>
    <div id="content[2]">yada yada</div>
    <div id="content[3]">rhubarb rhubarb</div>
    What I need is a function that can count the number of 'content' div tags - i.e. how large the content array is.

    I've taken a pre-written script and jigged with it so it does what I need but currently I need to manually update a variable holding how many content div tags there are which is an ugly solution.

    Can anyone help me out please? Thanks in advance!

    P.S. This is my script as it currently stands. Any advice is welcomed :
    Code:
    function DS_switchto(id)
        {
        var numberofpages=2;
        if (document.all)
            {
            for (z = 1; z <= numberofpages; z++)
                {
                document.all['content['+z+']'].style.display = 'none';
                }
            document.all['content['+id+']'].style.display = '';
            return false;
            } else if (document.getElementById)
            {
            for (z = 1; z <= numberofpages; z++)
                {
                document.getElementById['content['+z+']'].style.display = 'none';
                }
            document.getElementById('content['+id+']').style.display = 'block';
            return false;
            }
        }
    Last edited by solomon; 02-27-2005 at 07:15 AM.

  2. #2
    Join Date
    Nov 2002
    Location
    Dartmoor [Holiday]
    Posts
    2,382
    Perhaps something like this...

    Code:
    function switch2(id){
      divs = document.getElementsByTagName('div');
      for (var i=0; i<divs.length; i++)
        if (divs[i].id.substr(0, 7) == 'content')
          divs[i].style.display = (divs[i].id.substr(8, 1) == id ? 'block' : 'none');
    }
    Adam
    "If youíre not using valid HTML, then you havenít created a Web page. You may have created something else, but it isnít a Web page." - Joe Clark

    Do something amazing (USA) | Make Poverty History

  3. #3
    Join Date
    Feb 2005
    Location
    The dark place
    Posts
    91
    Excellent! that works a treat - thanks Adam.

    I was only expecting a system to count the div tags, but you clearly can't bear to see clumsy scripting

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles