1
\$\begingroup\$

I am setting a value to input box and trigger to hear the change. I would also like to always hear the input change, and for that I made this function.

Can any expert please verify the way I did is correct? Or is it further tune-able?

var page = function () {
 return {
 init : function () {
 var input = $("#pNo");
 $(document).on("pageChange", this.onPageChange.bind(this));
 $(input).val(1).trigger({type:'pageChange',element:input});
 $(input).bind("change paste keyup",function() { $.event.trigger({type: "pageChange", element:input})});
 },
 onPageChange : function (e) {
 console.log($(e.element).val());
 }
 }
}();
page.init();

Live Demo

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Dec 25, 2014 at 15:43
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

My suggestion is to reduce the code:

var page = {
 init: function(selector) {
 $(selector).on('change paste keyup', page.onPageChange);
 },
 onPageChange: function(e) {
 console.log($(e.target).val());
 }
};
page.init('#pNo');
answered Oct 15, 2015 at 14:39
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.