Loading ...
Sorry, an error occurred while loading the content.
 

get elements by class name - query ?

Expand Messages
  • carlosubuntu
    I want to get elements on my page by classname, but I only want to check for the 1st 4 letters of the classname, as they are all labeled like so: over_label
    Message 1 of 3 , Jul 2, 2008
      I want to get elements on my page by classname, but I only want to
      check for the 1st 4 letters of the classname, as they are all labeled
      like so:

      over_label
      over_lablelDisplay
      over_textbox

      etc....

      The below example worked for input fields that contain my prefix on
      the name attribute. What I need is similar to this, but using a class
      name vs the name attribute. And if I could search all elements rather
      than just input fields that would be even better. Any suggestions
      would be greatly appreciated


      item = YAHOO.util.Selector.query('input[name^=over]');
    • wally.ritchie
      ... You could define a new function: getElementsWithStringInClass = function ( s, tag, root, apply) { return YAHOO.util.Dom.getElementsBy(function (el){ return
      Message 2 of 3 , Jul 2, 2008
        --- In ydn-javascript@yahoogroups.com, "carlosubuntu" <carlosubuntu@...> wrote:
        >
        > I want to get elements on my page by classname, but I only want to
        > check for the 1st 4 letters of the classname, as they are all labeled
        > like so:
        >
        > over_label
        > over_lablelDisplay
        > over_textbox
        >
        > etc....
        >
        > The below example worked for input fields that contain my prefix on
        > the name attribute. What I need is similar to this, but using a class
        > name vs the name attribute. And if I could search all elements rather
        > than just input fields that would be even better. Any suggestions
        > would be greatly appreciated
        >
        >
        > item = YAHOO.util.Selector.query('input[name^=over]');
        >
        You could define a new function:

         getElementsWithStringInClass = function ( s, tag, root, apply) {
            return YAHOO.util.Dom.getElementsBy(function (el){
                   return el.className.match(s);}, tag,
                   root,  apply);
        };


        and to use it:

        var myElements = getElementsWithStringInClass("over_");

        You can optionally specify tag, root, and apply. See the documentation for YAHOO.util.Dom.

         This function returns an array of elements that have the supplied string s anywhere in the className string. If you include a tag it will get only elements with that tag. If you specify a root element, it will start at that root. If you include an apply function (with an element as the sole argument) then that function will be executed for each element.

        If you want to test that the classname is an actual prefix you'll have to get a bit fancier on the function (i.e. replace .match with a more elaborate test). Remember className is a string and can have multiple space separated classes.

        Hope this helps.
      • carlosubuntu
        ... Worked perfect. Thanks!!!!!!!!
        Message 3 of 3 , Jul 3, 2008
          --- In ydn-javascript@yahoogroups.com, "wally.ritchie"
          <wally.ritchie@...> wrote:
          >
          >
          > --- In ydn-javascript@yahoogroups.com, "carlosubuntu" <carlosubuntu@>
          > wrote:
          > >
          > > I want to get elements on my page by classname, but I only want to
          > > check for the 1st 4 letters of the classname, as they are all labeled
          > > like so:
          > >
          > > over_label
          > > over_lablelDisplay
          > > over_textbox
          > >
          > > etc....
          > >
          > > The below example worked for input fields that contain my prefix on
          > > the name attribute. What I need is similar to this, but using a class
          > > name vs the name attribute. And if I could search all elements rather
          > > than just input fields that would be even better. Any suggestions
          > > would be greatly appreciated
          > >
          > >
          > > item = YAHOO.util.Selector.query('input[name^=over]');
          > >
          > You could define a new function:
          >
          > getElementsWithStringInClass = function ( s, tag, root, apply) {
          > return YAHOO.util.Dom.getElementsBy(function (el){
          > return el.className.match(s);}, tag,
          > root, apply);
          > };
          >
          > and to use it:
          >
          > var myElements = getElementsWithStringInClass("over_");
          >
          > You can optionally specify tag, root, and apply. See the documentation
          > for YAHOO.util.Dom.
          >
          > This function returns an array of elements that have the supplied
          > string s anywhere in the className string. If you include a tag it will
          > get only elements with that tag. If you specify a root element, it will
          > start at that root. If you include an apply function (with an element as
          > the sole argument) then that function will be executed for each element.
          >
          > If you want to test that the classname is an actual prefix you'll have
          > to get a bit fancier on the function (i.e. replace .match with a more
          > elaborate test). Remember className is a string and can have multiple
          > space separated classes.
          >
          > Hope this helps.
          >
          Worked perfect. Thanks!!!!!!!!
        Your message has been successfully submitted and would be delivered to recipients shortly.