0

I am trying to retrieve the href attribute from a link. However, it keeps returning undefined.

This is the html, generated via ajax which I why the .on function does not work in jQuery

html += '<a class="viewOffer" href="singleOffer/'+ value.id + '">View offer</a></td>';

This is the jQuery function

$( ".viewOffer" ).click(function() {
 alert($(this).attr('href'));
});

Thanks!

S_NoBEl
  • 81
  • 1
  • 2
  • 11

1 Answers1

1
$( document ).delegate( ".viewOffer", "click", function() {
  alert($(this).attr('href'));
});

The delegate() method attaches one or more event handlers for specified elements that are children of selected elements, and specifies a function to run when the events occur.

Event handlers attached using the delegate() method will work for both current and FUTURE elements (like a new element created by a script).

Rino Raj
  • 6,264
  • 2
  • 27
  • 42