user385729
user385729

Reputation: 1984

click event for a href tag (which is created dynamically)

I'm trying to add a click event for a href tag (which is created dynamically and added by .html atributes to my result div).

What I have done so far:

$("#upload-div").on("a[href='#Sixty']",'click',function(){
   alert('test'); 
});

and

here you see my HTML which is added dynamically:

  <button type='button' class='btn btn-info dropdown-toggle' data-toggle='dropdown'>\n\
    Font Selection <span class='caret'></span>\n\
  </button>\n\
  <ul class='dropdown-menu' role='menu'>\n\
      <li><a href='#Arial'>Default(Arial)</a></li>\n\
      <li class='divider'></li>\n\
    <li><a href='#Arial'>Arial</a></li>\n\
    <li><a href='#Sixty'>Sixty</a></li>\n\
    </ul>\n\
</div>   \n\

But it does not work; did I miss anything? If you need more clarification, please let me know.

Thanks

Upvotes: 1

Views: 102

Answers (2)

Jason P
Jason P

Reputation: 27012

When using event delegation, the first param of on() is the event, the second is the selector:

$("#upload-div").on('click', 'a[href="#Sixty"]', function() {

Upvotes: 1

fanfavorite
fanfavorite

Reputation: 5199

Make sure the first param is the event like:

$("#upload-div").on('click','a[href="#Sixty"]',function(){
   alert('test'); 
});

You also want to make sure that upload-div is loaded when the above code is called.

Upvotes: 0

Related Questions