user765368
user765368

Reputation: 20346

get link from jquery object

I know this is probably very easy to do, but let's say I have something like this:

 $(document).on('click', 'a.my_link', function(){

       var my_anchor_object = $(this)     

 });

How do I get the actual anchor string:

<a href="test.com" id="my_link_id" class="my_link_class">My Link</a>

Of my link (not the jQuery object) from my_anchor_object?

Thank you

Upvotes: 0

Views: 58

Answers (2)

GautamD31
GautamD31

Reputation: 28763

Try like

var my_anchor_object = $(this).attr('href');     

and you can use this href for your anchor tag.If you want an entire tag try like

var my_anchor_ref = $(this).attr('href');
var my_anchor_txt = $(this).text();   
//Now append to a div
$('#my_div_id').append('<a href="'+my_anchor_ref+'">'+my_anchor_txt+'</a>');

Haha simply try with

var my_anchor = $(this).get(0);
$('#my_div_id').append(my_anchor);   

Upvotes: 5

Manish Jangir
Manish Jangir

Reputation: 505

Use the below code

$(document).on('click', 'a.my_link', function(){
   var my_anchor_object = $(this).clone().wrap('<div/>').parent().html();
    alert(my_anchor_object);
});

check at http://jsfiddle.net/Pyk4Y/25/

Upvotes: 1

Related Questions