Stian Stian - 7 months ago 34
Javascript Question

Selecting last dynamically added textbox

I have a list of input boxes with dates (and the class name "newDate") in them and I can add more of them dynamically. When i try to get the value of the last added textbox, I always end up with first, which was a textbox rendered with the page.

$(".newDate:last-of-type").val()


Do I need to change my selector to catch the dynamically added textboxes or is there something else that I need to think of here? Is the location of the script with the selector relevant?

Answer

As long as the selector is run once the textboxes have been added, the following will work:

$(".newDate").last().val()