user987055
user987055

Reputation: 1149

create an a link html and perform an action with jQuery?

I have a jquery that performs an action when you click on a.data. The problem is that when I create several a.data with another function, it does not perform the action.

I do not understand why the new a.data do not work?

This creates new a.data in divdata

var data_html  = '<a class="data" href="#" value="'data.id'">' + ref.info + '</a>';
$(data_html).prependTo( $('#divdata') );

This takes the action to the corresponding a.data

$('a.data').click( function (){
    $(this).attr('href');
    var idvalue = $(this).attr('value');
    idvalue *= 1;
    // aqui realizo  la acción
    console.log(idvalue);
 });

Upvotes: 0

Views: 64

Answers (2)

scrappedcola
scrappedcola

Reputation: 10572

http://api.jquery.com/on/ Your binding method only binds to the existing elements. You need to bind to the parent element using on for subsequent elements to be bound.

$("#divdata").on("click", "a.data", function(){
  // event stuff here
});

Upvotes: 1

Zoltan Toth
Zoltan Toth

Reputation: 47667

You have to delegate the event, like this:

$('#divdata').on('click', 'a.data', function (){
    $(this).attr('href');
    var idvalue = $(this).attr('value');
    idvalue *= 1;
    // aqui realizo  la acción
    console.log(idvalue);
 });

Upvotes: 2

Related Questions