title: Get id of a span in JavaScript.
body: <p>I want to get the span id in JavaScript following code always returning M26 but I want different values on different click M26 or M27:</p>
  
  <pre><code>function clickHandler() {
      var xid= document.getElementsByTagName("span");
      var xsp= xid[0].id;   
      alert(xsp);     
  } 
  }
  
  &lt;html&gt;
      &lt;BODY LANGUAGE = "javascript" onClick = "clickHandler();"&gt;
          &lt;a href="javascript:void(0)"&gt;&lt;u&gt;&lt;b&gt;&lt;span id=M26&gt;2011-  
          2012&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/a&gt;   
          &lt;div id=c26 STYLE="display:none"&gt;
              &lt;a href="javascript:void(0)"&gt;&lt;u&gt;&lt;b&gt;&lt;span id=M27&gt;2012-  
              2013&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/a&gt;   
          &lt;div id=c27 STYLE="display:none"&gt; 
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>