I'm trying to put a $(..).click(function () {...
inside a javascript object, but I feel like I'm asking for the impossible.
Inside this object there can be several Dom elements, so I think that making a jQuery extension is excluded too? But I admit, I can not understand how to make an jQuery extension, there are still concepts that escape me in this respect.
Anyway, here's an example of what I'm looking to do, even if it's not functional:
$(document).ready(function() {
class bt_Fab {
constructor($elem_ID, $messageBox_ID, message) {
this.$_ID = $elem_ID;
this.$_ID_MsgBox = $messageBox_ID;
this._msg = message;
this.$_ID.click(function() {
this.$_ID_MsgBox.text(this._msg); // does nothing...
});
}
};
var
myOneButton = new bt_Fab($('#btn_one'), $('#messageBox'), 'message from button one!'),
myTwoButton = new bt_Fab($('#btn_two'), $('#messageBox'), 'message from button two!');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id='btn_one'>button One</button>
<button id='btn_two'>button Two</button>
<!-- lot of html here -->
<div id="messageBox">... </div>
[ Update Edit ] ( for @Sam Hanley & @meagar )
This question is different from How to access the correct `this` inside a callback?
because this question is about the use of jQuery function inside a Javascript Object; and they are not about the corect usage of a this
in Javascript object model.