Don
Don

Reputation: 193

jquery target link click event

i create some links on the fly ...

 $('input[name="iplus"]').click(function() {  
    $(ol).append("<a href='#' title='delposition' class='beschr-"+($("#billsumary ol>li").length+1)+"'>l&ouml;schen</a>");  
}); 

now I like to target each created link like $('a[title='delposition']') and assign a click-event like:

$("a[title='delposition']").click(function() {
 alert("Link klicked ...");
});

...but this dont do it? Any suggestions?

Upvotes: 1

Views: 4379

Answers (2)

Nick Craver
Nick Craver

Reputation: 630409

You can assign the click handler when you create the element, like this this:

$('input[name="iplus"]').click(function() {  
  $("<a href='#' title='delposition' class='beschr-"+($("#billsumary ol>li").length+1)+"'>l&ouml;schen</a>")
  .click(function() {
    alert("clicked on");
  }).appendTo(ol);  
}); 

This builds the element, adds a click handler, then appends it to the ol object like your original code.

Upvotes: 1

Sarfraz
Sarfraz

Reputation: 382696

The live() method of JQuery should do the trick:

$("a[title='delposition']").live('click', function() {
 alert("Link klicked ...");
});

Upvotes: 0

Related Questions