Skip to content Skip to sidebar Skip to footer

Add Onclick Event To Hyperlink

I have a string containing my html content in code behind link like this

rrr

' 1.I need to add a onclick event

Solution 1:

Try this

$('p a').click(function(){
    alert($(this).prop('href'));
});

FIDDLE


Solution 2:

Try this

$("p a").click(function(e){
   e.preventDefault();
   var link = $(this).attr('href');
});

Solution 3:

Jquery with working jsbin - http://jsbin.com/azOSayA/1/edit

$("a").click(function(e){
 var a_href = $(this).attr('href');
  e.preventDefault();
 });

Solution 4:

Use

 $("p a").click(
      function(e) {
           e.preventDefault();
           $(this).attr("href"); //do something with this
      }
 );

This will add a onclick listener to every link within the page which is a child of a paragraph.

If the link is really important, you should probably give it an id, so that it can be identified uniquely.


Solution 5:

Try this,

$("p a").on('click',function(e){
   e.preventDefault();
   var link = $(this).attr('href');
   alert(link);
});

Post a Comment for "Add Onclick Event To Hyperlink"