0

I want to change the text inside of an element for dynamically created elements. i = 2 because that's Why is it not working?

var loanName = function() {
 for(var t=1; t < i; t++) {
  $('body').on('keyup', '.loanNameV'+t, function () {
   var loanN = $('.loanNameV'+t).val();
   $('.nameLoan'+t).text(loanN);
  });
 }
};
Frank
  • 73
  • 1
  • 1
  • 5
  • 1
    How does the HTML look like? Instead of binding events in a loop you can use *attribute starts with* selector `^=[..]` – Shaunak D May 04 '15 at 04:34
  • Related: http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example – Ram May 04 '15 at 04:39
  • Could you please provide your full html and script code so I can help you – Pratap Patil May 04 '15 at 04:40

1 Answers1

2
$('body').on('keyup', '[class^="loanNameV"]', function () {
         var numbesideclass = ($(this).attr('class').split('loanNameV'))[1];
         var loanN = $(this).val();
         $('.nameLoan'+numbesideclass).text(loanN);
});

Note: this code will work if you don't have another class for loanNameV elements like class="loanNameV1 anotherclass anotherclass" in this case this code will not work as expected

Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28