javascript - How do I attach a jQuery event to a object's method? -
i defined object properties , methods. can't find how attach jquery function method. want : when click on object #mytarget change html text.
i wrote :
function blocktest(word){ this.word = ""+genrandint(0, 25); this.applynewword = function(){ $(document).ready(function(){ $("#mytarget").click(function(){ $("#mytarget").html(this.word); }); }); }; }
actually, i'm trying
accomplishing want based on code provided isn't way go. i'd first create object
rather using function. insert attributes inside object , handle dom events separately:
(function($) { var obj = { word: "example word", applynewword: function() { return this.word; } } $(function() { $("#mytarget").on("click", function() { $(this).text(obj.applynewword()) }); }); }(jquery));
div { border: 1px solid black; cursor: pointer; padding: 5px; }
<div id="mytarget">dummy text</div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Comments
Post a Comment