Can't pass an input value into a javascript variable

If your code is writing to and depending on variables outside of the functions that do your heavy lifting, you’re in for a world of hurt. You should absolutely make your AJAX call in your button’s click handler. I blather almost without end on the topic in another post (good stuff starts in the second section).