[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

jquery function to focus on input field

Posted on 2011-10-11
5
Medium Priority
?
153 Views
Last Modified: 2012-05-12
Hi

i have a function that is called when the tab key is pressed on an input field
For some reason it only works when i puit an alert at the begginning.

See code attached...

Any ideas why?

Cheers
$("#CustomFields_1_1").keypress(function(event)
      {
        if (event.keyCode == 9)
        { 
          //only works if i put alert("hhhh");
          $("#CustomFields_2_1").css('display','block'); 
	  $("#newscompany").css('display','none');
          $("#CustomFields_2_1").focus();             
        }
      });

Open in new window

0
Comment
Question by:coolispaul
  • 2
  • 2
5 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 36951123
Page not ready.
You want to play with elements which are not present in your page.
0
 

Author Comment

by:coolispaul
ID: 36951187
but even if i have just the below:
 $("#newscompany").css('display','none');

it doesnt work. this must be on the page?

0
 
LVL 83

Expert Comment

by:leakim971
ID: 36951678
Use :

// checking :
alert( (($("#newscompany").length>0)?"field found":"field not found") );
$("#newscompany").css('display','none');
0
 
LVL 2

Accepted Solution

by:
WizardOfOgz earned 2000 total points
ID: 36951746
It is becoming common to avoid attaching jQuery event handlers using .keypress(...), .click(...), etc.

If it is the case that you are trying to bind an event handle before the element #CustomFields_1_1 is present, then you have a couple options.

Any of these should work for you
$(document).delegate("#CustomFields_1_1", "keypress", function(event){...});

// or

$("#CustomFields_1_1").live("keypress", function(event){...});

// or

$(function(){
  // Wait until the page has loaded to attach event handlers
  $("#CustomFields_1_1").keypress(function(event){...});
});

Open in new window

0
 

Author Comment

by:coolispaul
ID: 36955973
Hi

the following works when any key other than tab is pressed (in IE) - any idea why tab doesnt work?
 
$("#CustomFields_1_1").live('keydown', function (e) 
{ 
    $("#newscompany").css('display','none');
    $("#CustomFields_2_1").css('display','block'); 
    $("#CustomFields_2_1").focus();
    event.preventDefault();

});

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question