SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Atrribute value retrieving

    Hello. I have a collection of anchor elements (below code). I wish to retrieve the name of the anchor element which has onclick="say('425')" attribute i.e. Orange anchor? How can I achieve this? It is important for me to search the onclick value with the number 425 , then retrieve the anchor name.

    <a href="#" onclick="say('415')">Apple</a>
    <a href="#" onclick="say('425')">Orange</a>
    <a href="#" onclick="say('435')">Banana</a>
    Thanks in advance.

  2. #2
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello. I tried below code to convert the value of onclick attribute to string. But firebug warns me as below. What am I doing wrong?
    Code
    for (var i=0;i<3;i++) {

    alert(document.getElementsByTagName('a')[i].onclick.toString().search('425'));

    }

    Firebug warning
    document.getElementsByTagName("a")[i].onclick has no properties

  3. #3
    SitePoint Zealot coothead's Avatar
    Join Date
    Jan 2005
    Location
    Chertsey, London, England
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there owerio,

    try it like this...
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    <script type="text/javascript">
    window.onload=function() {
       anc=document.getElementsByTagName('a');
    for(c=0;c<anc.length;c++) {
       alert(anc[c].firstChild.nodeValue+' onclick=\n\n'+anc[c].onclick);
      }
     }
    </script>
    
    </head>
    <body>
    
    <div>
    <a href="#" onclick="say('415')">Apple</a>
    <a href="#" onclick="say('425')">Orange</a>
    <a href="#" onclick="say('435')">Banana</a>
    </div>
    
    </body>
    </html>
    
    coothead

  4. #4
    SitePoint Zealot coothead's Avatar
    Join Date
    Jan 2005
    Location
    Chertsey, London, England
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there owerio,

    I think that I may have misread your post.

    You probably want something like this...
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    <script type="text/javascript">
    window.onload=function() {
       number=425;
       anc=document.getElementsByTagName('a');
    for(c=0;c<anc.length;c++) {
    if(anc[c].onclick.toString().search(number)!=-1) {
       alert(anc[c].firstChild.nodeValue);
       }
      }
     }
    </script>
    
    </head>
    <body>
    
    <div>
    <a href="#" onclick="say('415')">Apple</a>
    <a href="#" onclick="say('425')">Orange</a>
    <a href="#" onclick="say('435')">Banana</a>
    </div>
    
    </body>
    </html>
    coothead

  5. #5
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @coothead, thank you so much. Your html works like a charm but I still have problem with my document. I use a document which read another html file by a php funciton to create content. Something wrong couldnt I find. By the way Firebug says anc[c].onclick has no properties about the document as error.


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
  •