SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2004
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ... getElementByClassName?

    hi,
    <div id="1" class="active">...

    i have this div in the html, knowing the class name, is it possible to know the respective id?
    something like...
    id = getElementByClassName('active');
    ??


    thanks

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    I asked the same question a year ago; maybe this can help .

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What's a 'respective id'? Any element can have any CSS class assigned.
    ::: certified wild guess :::

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2004
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by adios
    What's a 'respective id'?
    hi,
    sorry my english..."respective"/"correspondent".. don't know the correct term.

    thanks vinnie...
    i'm having problems figuring out that function..
    what can i do to make this work? the alert box should be displaying the id value...


    ----------------------------------------
    <html><head></head>
    <script language="javascript">
    function getElementsByClassName(className)
    {
    var arr = new Array();
    var elems = document.getElementsByTagName("*");
    for(var i = 0; i < elems.length; i++)
    {
    var elem = elems[i];
    var id = elem.getAttribute("id");
    var cls = elem.getAttribute("class");
    if(cls == className)
    {
    arr[arr.length] = id;
    }
    }
    return arr;
    }

    function alertId () {
    var Id = getElementsByClassName("active") ;
    alert(Id);
    }


    </script>
    <BODY onLoad="alertId()"><div class="active" id="1">&nbsp;</div></BODY></html>


    --------------------------------------------

  5. #5
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...sorry my english..."respective"/"correspondent".. don't know the correct term.
    Hey, wasn't commenting on your English (my Portuguese is non-existent) ..., just the concept of using a class name to locate an object. Noticed your earlier thread; you should keep that one alive, since it seems to be the same issue. Is your id "active" - or the class name? Can't use a number for an id.
    ::: certified wild guess :::

  6. #6
    SitePoint Zealot
    Join Date
    Mar 2004
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by adios
    Is your id "active" - or the class name? Can't use a number for an id.
    hi,
    ... class="active"...
    why can't i use number for id's?
    ...

  7. #7
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Frost
    hi,
    ... class="active"...
    why can't i use number for id's?
    ...
    Because it's against the rules of HTML 4.0 and XHTML 1.0. An ID can't start with a number

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...why can't i use number for id's?
    I don't know, I just live around here. Actually, it's a general concept, to keep various code parsers from confusing strings with actual numbers.

    Post what you've got so far, maybe with a description of exactly what you're trying to do. All this "id parsing" is almost always the wrong way to go about this: using Objects is the more logical approach.
    ::: certified wild guess :::

  9. #9
    SitePoint Zealot
    Join Date
    Mar 2004
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vgarcia
    An ID can't start with a number
    ... didn't know that...
    i'll have to make some changes around here...

    i replaced the id="1" for a string... and still not showing...

    any sugestion?...

  10. #10
    SitePoint Zealot
    Join Date
    Mar 2004
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by adios
    Post what you've got so far
    ...
    in the code above i have an onLoad function, that sends an alert box, what i wanted was for it to display the id of the Div wich has class="active"...(copy the code and test it)...
    if i can do this... then my problem is solved...

    thanks for the attention guys...

  11. #11
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The function returns an array, so...
    Code:
    alert(getElementsByClassName("active")[0])
    ::: certified wild guess :::

  12. #12
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's one that even works in IE4

    Code:
    function xGetElementsByClassName(clsName, parentEle, tagName) {
      var found = new Array();
      var re = new RegExp('\\b'+clsName+'\\b', 'i');
      var list = xGetElementsByTagName(tagName, parentEle);
      for (var i = 0; i < list.length; ++i) {
        if (list[i].className.search(re) != -1) {
          found[found.length] = list[i];
        }
      }
      return found;
    }
    function xGetElementsByTagName(tagName, parentEle)
    {
      var list = null;
      tagName = tagName || '*';
      parentEle = parentEle || document;
      if (xIE4 || xIE5) {
        if (tagName == '*') list = parentEle.all;
        else list = parentEle.all.tags(tagName);
      }
      else if (parentEle.getElementsByTagName) list = parentEle.getElementsByTagName(tagName);
      return list || new Array();
    }

  13. #13
    SitePoint Zealot
    Join Date
    Mar 2004
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    thanks

    thanks a lot guys!


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
  •