I have several <select> boxes all using the same prefix and I would like to set up a recursive function to essentially do the work for me.
$('[id^="by_"]').change(function(e)
{
var elem = e;
console.log(e.value);
});
Based on this code is my intention pretty clear? Am I on the right track?
console prints out: undefined
I think you’re on the right track – the selector you’re using matches a prefix of “by_”, and you’re binding the change event to all of them. Make sure you put this in
$(document).readyor similar. Are you having any problems with this code? Instead of using theeparameter, I would just usethisinside of the function to refer to the element and$(this)to get the jQuery object of it. So to get the value, you’d use:(ignore the
eandelemstuff, although it wouldn’t be a bad idea to store$(this)in something likeelemso you can have a reference to it instead of re-creating the jQuery object every time you need it)When using callbacks to events with jQuery, the (first) parameter of the callback is an
eventobject that explains many things about the event that occurred ( http://api.jquery.com/category/events/event-object/ ) and does not hold the element – that’s whatthisis for!