Wern Ancheta
Wern Ancheta

Reputation: 23297

How to get value of currently hovered anchor in jquery

What I am trying to do here is to get the value of the current link that is hovered over by the mouse:

    <script type='text/javascript' src='jq.js'></script>
    <script type='text/javascript'>

    $(function(){


    $('a').hover(function(){
    var imgName= $('a[href^=num]').val();
    alert(imgName);

    });




    });

What do I need to do here in order to make that happen?

    </script>
    </head>
    <body>

    <a href='numone'>numone</a>
    <a   href='numtwo'>numtwo</a>

Upvotes: 0

Views: 4080

Answers (3)

Chandu
Chandu

Reputation: 82903

Try this:

<script type='text/javascript' src='jq.js'></script>
<script type='text/javascript'>

    $(
        function()
        {
            $('a').hover
            (
                function()
                {
                    var imgName= $(this).attr("href");
                    alert(imgName);
                }
            );
    }
);
</script>

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074335

If you just want to get it when the mouse enters the element:

$(function(){

    $('a').mouseenter(function(){

        // Here, `this` points to the DOM element for
        // the anchor.

        // jQuery helps you get its contents, either as HTML:
        alert($(this).html());
        // Or text:
        alert($(this).text());

        // Or if you just want its href, id, etc., you can
        // just get that directly without a jQuery wrapper:
        alert(this.href);
        alert(this.id);
        alert(this.className);
    });

});

(I switched it to mouseenter since you weren't using the second half of hover, which is mouseleave.)

More in the jQuery docs:

Upvotes: 4

dheerosaur
dheerosaur

Reputation: 15172

Try .text()

Upvotes: 1

Related Questions