Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get jquery anchor href value

Tags:

jquery

anchor

jQuery:

$(document).ready(function() {
    $("a.change_status").click(function(){
       var status_id = $("a").val();
       alert(status_id); 
       return false;
    });
});

HTML:

<a href="?status=5" class="change_status">Aminul</a><br/>
<a href="?status=25" class="change_status">Arif</a><br/>
<a href="?status=15" class="change_status">Sharif</a><br/>

I need status_id and for some reason my anchor tag is dynamic. I can't use id or make class name dynamic. I think, I need to use $this to get my value.

like image 643
user1911703 Avatar asked Dec 18 '12 04:12

user1911703


People also ask

Can I pass value in anchor tag?

The only way to pass data with an anchor tag is to put them in the query string. If you have a lot of data and don't want it to appear in the address window, why do you want to use an anchor tag? You could always submit a form from the onclick event of an anchor tag.

How add href attribute in jQuery?

Answer: Use the jQuery . attr() Method You can use the jQuery . attr() method to dynamically set or change the value of href attribute of a link or anchor tag. This method can also be used to get the value of any attribute.

How do you remove a href?

We can remove the href by: Setting an empty href – ELEMENT. href = "" Or removing it entirely – ELEMENT.


3 Answers

$('a').attr('href');

should do the trick

like image 75
Rick Avatar answered Sep 21 '22 18:09

Rick


This one is simple :

     $(document).ready(function() {
         $("a.change_status").click(function(){
           var status_id = $(this).attr('href').split('=');
           alert(status_id[1]); 
           return false;
        });
    });

http://jsfiddle.net/NmzRV/

like image 17
hsuk Avatar answered Oct 21 '22 20:10

hsuk


var status_id= $(this).attr("href").match(/status=([0-9]+)/)[1];
like image 5
Bishnu Paudel Avatar answered Oct 21 '22 22:10

Bishnu Paudel