Reputation: 1156
What I expect here is the function to fire when I click the class, but it doesn't happen! The a.button is dynamically created but IS on the page when it's fully loaded!
Any ideas why?
HTML:
<a class="btn btn-success useImg">
<i class="fa-icon-plus"></i>
<span>Use</span>
</a>
Jquery:
$(".useImg").on("click", function(){
alert("test");
});
Upvotes: 0
Views: 63
Reputation: 458
Try using $("body").on("click", ".useImg", function(){
alert("test");
});
and it is recommended that you use latest jquery lib http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js
Upvotes: 0
Reputation: 707326
Your code works perfectly fine in a standalone jsFiddle which you can see here.
Therefore, you must have one of these issues:
You can protect against the first item by either putting your code in a $(document).ready()
handler or by using delegated event handling.
For the second item, you have to check your browser error console for script errors.
Upvotes: 4
Reputation: 18832
You need to add the click event to another element further up the DOM tree and then filter it to work on your anchor element. That will fire for any elements with the .useImg
class including those added dynamically.
$('body').on("click", ".useImg", function(){
alert("test");
});
Make sure you wrap the code in the jQuery DOM Ready event to ensure the page has fully loaded. eg
$(document).ready(function() {
$('body').on("click", ".useImg", function(){
alert("test");
});
});
Upvotes: 1
Reputation: 1523
$(document).ready(
$('.useImg').click(function(){ alert(test); })
)
Upvotes: 0
Reputation: 20209
Try, when document is ready
$(function() {
$(".useImg").on("click", function(){
alert("test");
});
}
Upvotes: 0