Link click function is not called

Asked

Viewed 78 times

3

I’ve had a similar problem, and that’s because the function is built before the button is created. Now I made little tertinho after I give an append on my page call a function on click.

That’s my code in the codepen: link

For you to understand the idea is that the person click on the black buttons to add fields of the desired type, they set the name of the field and label in the middle div, and in the third div is shown as this form.

What happens is that I want to be able to remove a field by clicking on the blue button, the problem is that it is not called that stretch:

$('#remove_input').on("click", function() {
  var count_field = $('#count_field').val();
  var parent_class = $(this).parents('tr');
  if (parent_class.hasClass('pending')) {
    parent_class.remove();
    count_field--;
  } else if(parent_class.hasClass('saved')) {
    parent_class.removeClass('saved').addClass('remove');
  }
  $('#count_field').val(count_field);
});

this code is inside the function click on the add button right after I make an append of the new field.

Someone can help me?

Just to make clear the address field that comes already in html this with the correct functioning that is just leave the red button. The problem is in the field added later, by the add button.

  • Your problem is probably because these things are dynamic. By creating the buttons you can copy a tag that is Hidden, or you make a bind passing the click event to the button to make it work. It’s just a kick, but I think that’s it.

  • @DH you could give an example with bind?

1 answer

0

See if it works.

$('#remove_input').click(function() {
    var count_field = $('#count_field').val();
    var parent_class = $(this).parents('tr');
    if (parent_class.hasClass('pending')) {
    parent_class.remove();
    count_field--;
    } else if(parent_class.hasClass('saved')) {
    parent_class.removeClass('saved').addClass('remove');
    }
    $('#count_field').val(count_field);
});

User the firebug to see if it is passing or if it is generating error.

  • Dude I’ve tried this and not for sure too, you tested in the codeopen? If yes sends me the link of a Fork.

  • In firebug it shows that it never calls that part of the code

Browser other questions tagged

You are not signed in. Login or sign up in order to post.