2

I've asked this question because of something I need to and I'm having some problems with jQuery. So I'm opening this question 'cause others may have this too.

In the answer posted in my other question, the user Felix tells me to do something like this:

$(document).keypress(function(e) {
    e.preventDefault();

    if(e.which == 13 ) {
        if($("#searchInput").is(":focus")) {
          alert("search form submitted");
          return false;
        } else {
          alert("save form submitted");                  
          return false;
        }
    }
});
​

That seems to be what I need, however, the condition if($("#searchInput").is(":focus")) is always returning True, no mather what control is focused.

Anyone has a clue why this might be happening or any other way I could do this?

Community
  • 1
  • 1
gabsferreira
  • 3,089
  • 7
  • 37
  • 61

3 Answers3

2

It might have been caused by older version of jquery used. If it's older than 1.6 then :focus won't work if you havent defined it. Version 1.6 and further have the :focus selector, we don't have to define it separately. So just upgrade your library version if it's old or define :focus somewhere in your js file. Check api for 1.6 here: jquery version 1.6

gabsferreira
  • 3,089
  • 7
  • 37
  • 61
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
2

Your jQuery version might be out of date. Get the latest version with:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
Tom G
  • 3,650
  • 1
  • 20
  • 19
0

My jQuery version was out of date, I just got the latest version from here and now that code is running ok.

gabsferreira
  • 3,089
  • 7
  • 37
  • 61
  • and dev make sure that your other module is not getting affected due to change in library.i wrote validation for all controls.when i upgraded my library from 1.4.1 to 1.8.2 i saw weird behavior of some controls not following proper validation. – Milind Anantwar Oct 16 '12 at 06:03
  • Right, I'll check that. Thank you. – gabsferreira Oct 16 '12 at 13:35