user3662697
user3662697

Reputation: 147

access dynamically generated id

Say you are generating an id this way

var id = Date.now();
$("#someID").click(function(){
  var content = '<input type="text" id ="test[' + id + '].address"';
  $('#contentArea').append(content);
});

SomeID might be clicked a couple times thus generating many inputs, how can you trigger the onkeydown event on each?

Upvotes: 1

Views: 80

Answers (2)

Dima Daron
Dima Daron

Reputation: 568

If i understand you correctly you what to catch on change event of dynamically created input. So you can add class="my_class" to input select class and use :

$('.my_class').on('change',function(){...});

Use $(this) to access the current input.

Upvotes: 2

j08691
j08691

Reputation: 208040

You can use .on()'s event delegation syntax:

$(document).on('keydown','input[id^="test"]',function(){...});

Ideally you want to target an existing ancestor in the DOM closer than just document, but that will still work.

Upvotes: 1

Related Questions