I use the following structure to set out my objects/classes in JavaScript:
SelectUser = function(instanceID) {
this._instanceID = instanceID;
// Initialize
this.initialize();
}
SelectUser.prototype = {
initialize: function () {
...
},
update(userID) {
$('#hidden-field-' + this._instanceID).val(userID);
}
}
This allows me to say:
$selectUser = new SelectUser(1);
Outside of the SelectUser
object I need to execute some different code (per instance of SelectUser
) each time the value of the hidden field is changed. My first idea was to try:
<script type="text/javascript">
$(document).ready(function () {
$selectUser = new SelectUser(1);
$selectUser2 = new SelectUser(2);
$('#hidden-field-1').change(function () {
alert('Something');
});
$('#hidden-field-2').change(function () {
alert('Something else');
});
});
</script>
However the alert is not triggered. My next thought was to add an event trigger on my update function/method within the SelectUser
object/class. Then I can subscribe to this event per each instance and execute some different code.
How do I do this? I've been using JavaScript for years but I'm fairly new to jQuery and OOP in JavaScript.