SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)

    Getting ID from object being clicked

    Is it possible to get the ID of an object being clicked, without adding an onclick element to the object (only to the BODY tag)?

    What I'm looking for is something like
    HTML Code:
    <html>
      <head>
        <script type="text/javascript">
          function getClickedId() {
            var clickedId = ???
          }
        </script>
      </head>
      <body onclick="getClickedId()">
        <div id="exampleId">Example</div>
      </body>
    </html>
    Basically, when I click on the displayed text 'Example', I want the variable 'clickedId' to have the value 'exampleId'.

    Is this possible? I've tried looking Googleing around, but with no luck.
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!

  2. #2
    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 C. Ankerstjerne,

    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">
    
    <style type="text/css">
    <!--
    div {
        width:300px;
        line-height:30px;
        margin:10px auto;
        text-align:center;
        border:1px solid #000;
     }
    -->
    </style>
    
    <script type="text/javascript">
    <!--
    window.onload=function() {
       var myId;
       obj=document.getElementsByTagName('*');
    for(c=0;c<obj.length;c++) {
    if(obj[c].id!='') {
       obj[c].onclick=function() {
       myId=this.id;
       alert(myId);
        }
       }
      }
     }
    //-->
    </script>
    
    </head>
    <body>
    
    
    <div id="div1">this div has an id</div>
    <div>this div does not have an id</div>
    <div id="div2">this div has an id</div>
    <div>this div does not have an id</div>
    <div id="div3">this div has an id</div>
    <div>this div does not have an id</div>
    <div id="div4">this div has an id</div>
    <div>this div does not have an id</div>
    
    </body>
    </html>
    coothead

  3. #3
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Brilliant - exactly what I was looking for! Thanks!
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!


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
  •