0

on dom ready I have wired event

$('.someClass').on('input', function (e) {
   //  do something
});

on same I'm injecting html elements with where I add .someClass to include that field for same event

var cssClass = "form-control";
if (myProperty == true) {
     cssClass = "form-control someClass";
}
('#myTable tr:last').after(
   '<tr>'+
   '<td><input class=' + cssClass + ' type="text"'</td></tr>'+
   '</tr>'
);

but I'm getting rendered inside firebug as

<input class="form-control someClass" type="text"</td someClass="">

and this field is not fetched on .someClass event

user1765862
  • 13,635
  • 28
  • 115
  • 220
  • Half dupe of [Event binding on dynamically created elements?](http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – Tushar Dec 15 '15 at 14:02

2 Answers2

1

the solution is to write:

$('body').on('input','.someClass', function (e) {
   //  do something
});

and fix your code:

$('#myTable tr:last').after(
   '<tr>'+
   '<td><input class=' + cssClass + ' type="text"/></td></tr>'
);
MoLow
  • 3,056
  • 2
  • 21
  • 41
0

Mmm... aren't you missing the quotation mark (") in the input's class?

Furthermore, you are not correctly appending the td and tr closing tags. Try something like this:

$('#myTable tr:last').after(
    '<tr>' +
    '<td><input class="' + cssClass + '" type="text" /></td>' + 
    '</tr>'
);