JQuery get binding value

I'm trying to get the value of a clicked anchor, but I'm not sure which anchor is clicked or how to hook on it using jquery. I have a document.ready function

$(document).ready(function () {
var ActiveBlogStats = $('#BlogSelectList');
$('#BlogSelectList').click(function () {
    alert(ActiveBlogStats.text)
}); })

Here is the id I'm working on

<ul class="submenu" id="BlogSelectList">
  <xsl:for-each select="oohru/user/oohblog">
    <li>
      <a>
        <xsl:attribute name="href">#<xsl:value-of select="normalize-space(blogid)"/></xsl:attribute>
        <xsl:value-of select="oohblogurl"/>
      </a>
    </li>
  </xsl:for-each>
<li><a href="AddNewBlog.aspx">+ Create a new oohblog</a></li>
</ul>

How can I get href from the actual anchor? I do not see how to make document.ready even for elements that do not exist until the document is ready. I believe that I could get the # value from the URL, since clicking on it will update the URL, but I would rather get it directly from the click.

+3
source share
3 answers

, .live() ( delegate()) (http://api.jquery.com/live/ | http://api.jquery.com/delegate/)

:

    $("#BlogSelectList li a").live('click', function () {
        //do something
    });
+1
$("#BlogSelectList a").click(function(){

var href = $(this).attr("href");



});
+1

You can get href with something like this:

$("#BlogSelectList").delegate("a", "click", function() {

    var href = $(this).attr("href");
    alert(href);

});

And if you need to stop it from being redirected, you can do:

$("#BlogSelectList").delegate("a", "click", function(e) {

    e.preventDefault();

    var href = $(this).attr("href");
    alert(href);

});
0
source

All Articles