Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the clicked link's href with jquery?

Tags:

jquery

Does anyone know how can I get the clicked link's href with jquery? I have the link as following:

    <a  href="ID=1" class="testClick">Test1.</a>     <br />     <a  href="ID=2" class="testClick">Test2.</a>     <br />     <a  href="ID=3" class="testClick">Test3.</a> 

I wrote a code as following to get the href value from the link I clicked on. But somehow this is always return me the 1st link's href (ID=1) even though I clicked on Test2 or Test3. Does anyone know what is it going on here? and how can I solve this issue?

    $(".testClick").click(function () {         var value = $(".testClick").attr("href");         alert(value );     }); 
like image 620
Jin Yong Avatar asked Apr 01 '11 00:04

Jin Yong


People also ask

How to get clicked anchor tag value in jQuery?

In jQuery, you can use the . prop() method to get the href value of an anchor tag.

How to click href jQuery?

In jQuery, the click href is defined as by clicking the link it will direct to the content in which that link contains and when this link is clicked the click event takes place and this is done by triggering the click function which is an in-built function in jQuery.

How can I get the text value of a clicked link?

$('#my-div a'). click(function() { var txt = $(this). text(); console. log(txt); });


1 Answers

this in your callback function refers to the clicked element.

   $(".addressClick").click(function () {         var addressValue = $(this).attr("href");         alert(addressValue );     }); 
like image 180
Daff Avatar answered Sep 29 '22 16:09

Daff