Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    553
    Thanks
    0
    Thanked 0 Times in 0 Posts

    get element text where only class is available

    I need to get the values of some items within an html document where the structure of the html is as below (the page is generated by software and I cannot alter the html). I know there are funtions for getElementsByClass, but I cannot seem to get the innerhtml value from the element below. Is there a way to do this?

    <ul class="keys">
    <li class="key"><span><a class="key-link" href="keys.html">News</a></span></li>
    </ul>

    G

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Ummm...WHICH "element below"??? The UL, the LI, or the A??

    And what did you try??

    Can you show code?

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    553
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, OP. I am trying to get the values, the innerhtml, of the links. So in the examples, I want to retrieve the word 'news' into a variable. I used a standard function for get elements by class name. I tried:
    <script type="text/javascript">
    var lnx = getElementsByClass(key-link);
    for(i=0; i<lnx.length; i++){
    alert(lnx[i]);
    }
    function getElementsByClass( searchClass, domNode, tagName) {
    if (domNode == null) domNode = document;
    if (tagName == null) tagName = '*';
    var el = new Array();
    var tags = domNode.getElementsByTagName(tagName);
    var tcl = " "+searchClass+" ";
    for(i=0,j=0; i<tags.length; i++) {
    var test = " " + tags[i].className + " ";
    if (test.indexOf(tcl) != -1)
    el[j++] = tags[i];
    }
    return el;
    }
    </script>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •