title: jQuery get hash of <a> element using .on() event
body: <p>Before I get flamed with references to manuals, I have been researching this for quite some time, and keep getting dead ends.  Not even sure how to debug it properly.  So if any references are cited, please ensure they are well-commented and instructional.  This due to me being a PHP/mySQL developer, and the JavaScript object reference model is a bit confusing to me (but definitely doing my best to learn! :) ).</p>
  
  <p>I am trying to update my knowledge of jQuery using jQuery v1.7.2.  Apparently the live event has been deprecated, and has been replaced by .on().  The jQuery docs say not to blend old 'click' or 'live' events with any scripts using .on().</p>
  
  <p>While trying to get used to using on, I am trying to capture the hash of a given url.</p>
  
  <p>The page is as follows (header has already loaded jquery.min.1.7.2.js):</p>
  
  <pre><code>    &lt;div id='menuHolder' style="position:relative; margin-left:50px;margin-bottom:30px; padding-top:35px; min-width:600px;z-index:998;"&gt;
        &lt;ul id="menu"&gt;
           &lt;li&gt;&lt;a href="#"&gt;Events&lt;/a&gt;
              &lt;ul id="events"&gt;
                  &lt;li&gt;
                      &lt;img class="corner_inset_left" alt="" src="images/menu/corner_inset_left.png"/&gt;
                      &lt;a id="linker21 menu" class="test AJAXcaller" href="index.php#?p=1&amp;amp;d=AJAXcontent&amp;i=1&amp;amp;k=&lt;?=$_SESSION['authcpanel']['key']?&gt;" rel="nofollow"&gt;Upcoming Events&lt;/a&gt;
                      &lt;img class="corner_inset_right" alt="" src="images/menu/corner_inset_right.png"/&gt;
                  &lt;/li&gt;
                  &lt;li&gt;&lt;a href="#"&gt;List All Events&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a id="linkermenu" class="test AJAXcaller" href="index.php#?p=24&amp;amp;d=AJAXcontent&amp;i=1&amp;amp;k=&lt;?=$_SESSION['authcpanel']['key']?&gt;" rel="nofollow"&gt;Add Events&lt;/a&gt;&lt;/li&gt;
                  &lt;li class="last"&gt;
                      &lt;img class="corner_left" alt="" src="images/menu/corner_left.png"/&gt;
                      &lt;img class="middle" alt="" src="images/menu/dot.gif"/&gt;
                      &lt;img class="corner_right" alt="" src="images/menu/corner_right.png"/&gt;
                  &lt;/li&gt;
              &lt;/ul&gt;
           &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
  
  &lt;script&gt;
      $(document).on("click", "a.AJAXcaller", function(){ 
          alert("Goodbye!");
          alert($(this).attr("hash"));
      }); 
  &lt;/script&gt;
  </code></pre>
  
  <p>The first alert fires on clicking the 'Add Events' link (which has a hash), relays 'Goodbye!', then the second handler fires and relays 'undefined'.  How would I access this hash?</p>
  
  <p>My old code is as follows, and it works, but any ajax called pages won't have event handlers attached, which is why I'm using the .on() event.  Also, If I'm going to learn how to do it PROPERLY this time ;), I don't want deprecated functions involved...</p>
  
  <p><em><strong>OLD CODE(works)</em></strong></p>
  
  <pre><code>var linkClickAction = {
          'a.AJAXcaller' : function(element){
              element.onclick = function(){
                  var locationString = $(this).attr("hash");
                  locationString = locationString.replace(/.*\#(.*)/, "$1")
              var parameterString = locationString.replace(/.*\?(.*)/, "$1"); // onclick="sndReq('j=1&amp;q=2&amp;t=127.0.0.1&amp;c=5');
  
              var parameterTokens = parameterString.split("&amp;"); // onclick="sndReq('j=1,q=2,t=127.0.0.1,c=5');
              var parameterList = new Array();
              for (j = 0; j &lt; parameterTokens.length; j++) {
                  var parameterName = parameterTokens[j].replace(/(.*)=.*/, "$1"); // j
                  var parameterValue = parameterTokens[j].replace(/.*=(.*)/, "$1"); // 1
                  parameterList[parameterName] = parameterValue;
              }
              var page = parameterList['p'];
              var key = parameterList['k'];
              var includesDir = parameterList['i'];
              var changeDiv = parameterList['d'];
  
              sndReq(page,key,includesDir,changeDiv,parameterString); 
              return false;       
              }
          }
  
      };
  Behaviour.register(linkClickAction);
  </code></pre>
  
  <p>There was also a behaviour.js script loaded that basically handled the listener.  I wasn't using jQuery at all at that time.  It was nice, and FAST(tiny .js files, no extraneous code), comparatively speaking, but I hit my limit of being able to write effective JavaScript when it came to handling fancy things in my ui.  So I have to load the jQuery script at page load anyways.  Seeing as how it's already loaded, I'm trying to make use of it's functions instead of adding more unnecesarry scripts for my page to load.  Thus I want to find a result that uses native jQuery 1.7.2 functions to accomplish this goal.</p>
  
  <p><em><strong>RESULTS</em></strong></p>
  
  <p>It turns out that jQuery 1.6+ can break when using the .attr() to find a property of a DOM object.  So using .prop() was the way to go.  Corrected code is as follows:</p>
  
  <pre><code>/*
  Page:           rating.js
  Created:        Aug 2006
  Last Mod:       May 30, 2012
  Modder:         Darren Maurer
  */
  
      function sndReq(page,key,includesDir,changeDiv,parameterString) {
          var divToChange = document.getElementById(changeDiv); // the Div that the data will be put into
  
          // switch Div with a loading div
          divToChange.innerHTML = '&lt;div class="loading"&gt;&lt;img src="images/loading.gif" title="Saskatoon.CityGuru.ca - Loading Page" alt="Saskatoon.CityGuru.ca - Loading Page" border="0"/&gt;&lt;/div&gt;';
  
          if (includesDir == 1){
              //Find Current Working Directory.  Use to find path to call other files from
              /*var myloc = window.location.href;
              var locarray = myloc.split("/");
              delete locarray[(locarray.length-1)];
              var arraytext = locarray.join("/");
              */
              $.ajax({
                  type: "POST",
                  url: "AJAXCaller.php",
                  data: parameterString,
                  success: function(msg){
                      $('#'+changeDiv).html(msg);
                  }
              });
          } 
      }
  
  /* =======END AJAX FUNCTIONS================= */
  
  /* =======BEGIN CLICK LISTENER FUNCTIONS================= */
  /* Listens to any a href link that has a class containing ' AJAXcaller' */
  /* ie. &lt;a id="link1" class="test AJAXcaller" href="index.php#http://233.help?id=22&amp;think=33" rel="nofollow"&gt;&gt; AJAX TEST LINK &lt;&lt;/a&gt; */
  $(document).on("click", "a.AJAXcaller", function(){
      alert($(this).prop("hash")); 
              var locationString = $(this).prop("hash");
              locationString = locationString.replace(/.*\#(.*)/, "$1")
              var parameterString = locationString.replace(/.*\?(.*)/, "$1"); // onclick="sndReq('j=1&amp;q=2&amp;t=127.0.0.1&amp;c=5');
  
              var parameterTokens = parameterString.split("&amp;"); // onclick="sndReq('j=1,q=2,t=127.0.0.1,c=5');
              var parameterList = new Array();
              for (j = 0; j &lt; parameterTokens.length; j++) {
                  var parameterName = parameterTokens[j].replace(/(.*)=.*/, "$1"); // j
                  var parameterValue = parameterTokens[j].replace(/.*=(.*)/, "$1"); // 1
                  parameterList[parameterName] = parameterValue;
              }
              var page = parameterList['p'];
              var key = parameterList['k'];
              var includesDir = parameterList['i'];
              var changeDiv = parameterList['d'];
              sndReq(page,key,includesDir,changeDiv,parameterString); 
              return false;   
  }); 
  </code></pre>
  
  <p>hope that helps someone, it definitely was a lifesaver for me!</p>