My selector is:
$('section#attendance input:last')
However, I append another input to section#attendance. I want the selector to now select that element (as it should, because of the :last
. However, for some reason, it doesn't, and I'm not too sure why?
Here's my full code:
$('section#attendance input:last').keydown(function(e) {
if (e.which == 9)
{
var $this = $(this);
var num = parseInt($this.attr('name').match(/\d+(,\d+)?/)[0]) + 1;
$this.parent().append('<input type="text" name="attendance[' + num +']" value="Name and Position" class="medium" />');
}
});
New Code:
$("section#attendance").on("keydown", "input:last", function(e) {
if (e.which == 9) {
var $this = $(this);
var num = parseInt($this.attr('name').match(/\d+(,\d+)?/)[0]) + 1;
$this.after('<input type="text" name="attendance[' + num +']" value="Name and Position" class="medium" />');
}
});
EDIT: The issue seems to be on the 'input:last', as it works if I just use input. Also, if I add class 'last' to the last element, it works when I use 'input.last' as a selector.