SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Finding number of elements on a page

    Hi,
    I have a number of elements on a page, which I want to remove. The number changes depending on the number of the results,

    ie. div Id like result1, result2, result3 etc.

    Is there a way for me to find out the number of items called resultX or is there a way for me to search through all ID's like result ?


    Thanks in advance.

  2. #2
    Learning...
    Join Date
    Jan 2003
    Posts
    778
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    pure javascript:
    Code:
    <script type="text/javascript">
        window.onload = function(){
            var divs=document.getElementsByTagName('div')
            for (var i=0;i<divs.length;i++){
                alert(divs[i].id);
            }
        }
    </script>
    html
    Code:
    <div id="test1"></div>
    <div id="test2"></div>
    <div id="test3"></div>
    <div id="test4"></div>
    <div id="test5"></div>
    <div id="test6"></div>
    <div id="test7"></div>
    And jQuery:
    Code:
    <script type="text/javascript">
        $(document).ready(function(){
            $('#hideDivs').click(function(){
                $('.hideMe').hide();
            });
        });
    </script>
    html
    Code:
    <div id="test1">one</div>
    <div id="test2" class="hideMe">two</div>
    <div id="test3">three</div>
    <div id="test4">four</div>
    <div id="test5" class="hideMe">five</div>
    <div id="test6">six</div>
    <div id="test7">seven</div>
    
    <a href="#" id="hideDivs">Hide Divs</a>
    The beauty of life is not dependent on how happy you are,
    but on how happy others can be because of you...

  3. #3
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cool, thanks for your help.


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
  •