javascript function get id value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Edgardo R. Del Rosario

    javascript function get id value

    Is there a function in JavaScript to get the list of the values for all
    the 'id' attributes in an HTML page?
  • RobG

    #2
    Re: javascript function get id value

    On Aug 1, 11:11 am, "Edgardo R. Del Rosario" <axd...@psu.edu wrote:
    Is there a function in JavaScript to get the list of the values for all
    the 'id' attributes in an HTML page?
    No.

    You will have to sift through the elements on the page and find those
    that have an id attribute with an appropriate value, e.g. where the id
    attribute value has a length other than zero, something like:

    if (element.id && element.id.leng th 0)

    If you wish to disginguish between those that have the attribute but
    no value and those that don't have the attribute at all, e.g. between

    <span id="" ...>

    and

    <span ...>


    in a consistent, cross-browser way then you have a bigger challenge.


    --
    Rob

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: javascript function get id value

      Edgardo R. Del Rosario wrote:
      Is there a function in JavaScript to get the list of the values for all
      the 'id' attributes in an HTML page?
      No, but you may have an XPath API available which allows you to get a result
      containing all element objects representing elements that have the `id'
      attribute set. (This is considerably faster that traversing the document
      tree yourself, but it is also less compatible.) In its simplest form, which
      works for HTML documents in Gecko-based UAs:

      var elemsWithId = document.evalua te(
      "//*[@id or @iD or @Id or @ID]", document.docume ntElement, null,
      XPathResult.UNO RDERED_NODE_ITE RATOR_TYPE, null);

      From this result, you can create the list of IDs:

      var e, list = [];
      while ((e = elemsWithId.ite rateNext())
      {
      list[list.length] = e.id;
      }

      Or, suppose you are dealing with invalid markup where duplicate IDs may occur:

      var e, ids = new Object(), list = [];
      while ((e = elemsWithId.ite rateNext())
      {
      ids[e.id] = true;
      }

      for (var id in list)
      {
      list[list.length] = id;
      }

      In any case, you can then sort the list:

      list.sort();


      HTH

      PointedEars
      --
      Use any version of Microsoft Frontpage to create your site.
      (This won't prevent people from viewing your source, but no one
      will want to steal it.)
      -- from <http://www.vortex-webdesign.com/help/hidesource.htm>

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: javascript function get id value

        Thomas 'PointedEars' Lahn wrote:
        [...]
        Or, suppose you are dealing with invalid markup where duplicate IDs may occur:
        >
        var e, ids = new Object(), list = [];
        while ((e = elemsWithId.ite rateNext())
        {
        ids[e.id] = true;
        }
        >
        for (var id in list)
        Must be

        for (var id in ids)

        (I renamed the variable name afterwards, but not everywhere.)


        PointedEars
        --
        Prototype.js was written by people who don't know javascript for people
        who don't know javascript. People who don't know javascript are not
        the best source of advice on designing systems that use javascript.
        -- Richard Cornford, cljs, <f806at$ail$1$8 300dec7@news.de mon.co.uk>

        Comment

        • David Mark

          #5
          Re: javascript function get id value

          On Jul 31, 9:27 pm, RobG <rg...@iinet.ne t.auwrote:
          On Aug 1, 11:11 am, "Edgardo R. Del Rosario" <axd...@psu.edu wrote:
          >
          Is there a function in JavaScript to get the list of the values for all
          the 'id' attributes in an HTML page?
          >
          No.
          >
          You will have to sift through the elements on the page and find those
          that have an id attribute with an appropriate value, e.g. where the id
          attribute value has a length other than zero, something like:
          >
          if (element.id && element.id.leng th 0)
          The second test is redundant and the first can throw exceptions.
          Better to do this:

          if (typeof element.id == 'string' && element.id)

          Comment

          • Edgardo R. Del Rosario

            #6
            Re: javascript function get id value

            Edgardo R. Del Rosario wrote:
            Is there a function in JavaScript to get the list of the values for all
            the 'id' attributes in an HTML page?
            Thank you everybody... great suggestions! let me try them and I might
            be talking to you soon! Thanks!

            Comment

            Working...