TypeError: $(…).on is not a function

Hi Experts,

I want to call a function in jquery when mouse click or key press is occured.  one way is to write same code twice for this both event but it is not proper way. can any one tell me how it is possible? When I create jquery function and have it registered for both events, it gave me an error saying :
TypeError: $(…).on is not a function

Here is my code:
$(".btnchec").on("click mouseenter","input",function(){
                   alert('done');
                  var strCPF = jQuery("#inputcpf").val();
                  console.log(TestaCPF(strCPF));
            });
Bade011Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Prakash SamariyaIT ProfessionalCommented:
You can try something like belows: (assume "a" and "b" is the element

$.each([a, b], function()
{
    this.click(function () { });
});

OR
$([a, b]).on('click', function() {
      // your logic
});

OR
$(a & b).click(function() {
     // your logic
});
0
Bade011Author Commented:
Hi Prakash,
I have only one element. i.e input box . I want to use mouse click and keyboard enter properties both for that textbox to validate the cpf number that the user entered. In other words, i want both enter and click to work for validating the input box. I hope I made clear to my question
0
Tom BeckCommented:
A "TypeError" can occur if the function you are calling is not recognized. jQuery "on" was added to jQuery v1.7. If your jQuery library version is older than that it will generate that error.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Bade011Author Commented:
Hi Tom,
Thanks. ya it stopped giving me that error; however, the function for both mouse-click and key-press didn't work; whereas, it worked when I created 2  functions for keypress and mouseclick separately. I guess my question is which selector should i keep when I combine function for both? Following is the code : where #inputcpf = inputbox's id and .btnchec= button class

(function($) {                              //Both events in one function
      $("#inputcpf").on("click keypress",function() {
            var strCPF = jQuery("#inputcpf").val();
            console.log(TestaCPF(strCPF));
      })(jQuery)});
      
      //jQuery(".btnchec").click(function(){                   //separate function for Mouseclick event
            //var strCPF = jQuery("#inputcpf").val();
            //console.log(TestaCPF(strCPF));
      //});
      
      //$("input").keypress(function(event) {                  //separate function for Keypress event
      //    var strCPF = jQuery("#inputcpf").val();
            //console.log(TestaCPF(strCPF));
      //});
0
Tom BeckCommented:
Just use the class name for the inputs and create one function to handle both 'click' and 'keypress'.

http://jsfiddle.net/z8tt5ho7/
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bade011Author Commented:
Ahh I know what you are showing. Its for same input box. My project might be weird one. As I had separate button to validate. I have a textbox to get user input which i need to validate if user press enter key. Also there is a button which will validate the input if mouse click that button. I need to display a message saying "CPF Inválido" if the enter cpf number is wrong. But its not working.  I am keeping my code here just incase if you think you want to check or help otherwise thank you so much Tom. I really appreciate it.
http://jsfiddle.net/Lodtssfy/
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.