script that returns elements matching css selectors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kae Verens

    #1

    script that returns elements matching css selectors

    Anyone know of a script which returns a list of elements matching a
    specified CSS selector?

    Kae
  • Kae Verens

    #2
    Re: script that returns elements matching css selectors

    Kae Verens wrote:
    [color=blue]
    > Anyone know of a script which returns a list of elements matching a
    > specified CSS selector?[/color]

    hehe! I was thinking exactly of how Dean Edwards' IE7 script selects
    elements, and low and behold, the man himself has exactly the script I need
    - cssQuery().

    Thanks anyway.

    kae

    Comment

    • RobG

      #3
      Re: script that returns elements matching css selectors

      Kae Verens wrote:[color=blue]
      > Kae Verens wrote:
      >
      >[color=green]
      >>Anyone know of a script which returns a list of elements matching a
      >>specified CSS selector?[/color]
      >
      >
      > hehe! I was thinking exactly of how Dean Edwards' IE7 script selects
      > elements, and low and behold, the man himself has exactly the script I need
      > - cssQuery().
      >
      > Thanks anyway.
      >
      > kae[/color]


      You may like Dean's script, but if your needs are simpler, here
      is a script that returns all the elements with a certain class
      name, even if an element has multiple class names. To make it
      faster, pass a start element so search below (it currently
      starts at body) to reduce the scope of the recursion.

      It returns an array of the matched objects:


      function getElementsByCl assName(c) {
      var cArray = [];
      var n = document.getEle mentsByTagName( 'body')[0];
      var z = new RegExp('\\b' + c + '\\b');

      function doTree(n) {
      if (n.className && z.test(n.classN ame)){
      cArray.push(n);
      }
      for (var i=0, len=n.childNode s.length; i<len; i++) {
      doTree(n.childN odes[i]);
      }
      }
      doTree(n);
      return cArray;
      }



      --
      Rob

      Comment

      Working...