aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes focusing on an input element with jquery Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "focusing on an input element with jquery" Watch "focusing on an input element with jquery" New topic
Author

focusing on an input element with jquery

Naveen puttu
Ranch Hand

Joined: Sep 15, 2009
Posts: 88
Hi Folks

i tried using and it doesnt seem to work . so i changed it to what you see in the code below



basically i want the control to go back the same text box if a inappropriate value is entered . Am i missing anything

Cheers
n4v33n

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

Naveen puttu wrote:i tried using and it doesnt seem to work


Of course not. What's returned from the $() function is a jQuery wrapped set, upon which you must call jQuery methods. The focus() method that (I think) you are trying to use is defined on the DOM elements, not jQuery. So in order to use focus, you must obtain the element from the jQuery wrapped set.

One way:
This uses the jQuery get() method to obtain the element.

The jQuery wrapped set also acts like an array, so you could also use:

By the way using [id=whatever] in a selector would be considered sloppy, use the # notation when obtaining elements by id.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

The previous reply assumes that you want to set focus to the element. Is that true or not?

There is a focus() method for jQuery, but that does something very different: assigns an event handler for focus events.

Which did you intend to use? I had assumed the former.
Naveen puttu
Ranch Hand

Joined: Sep 15, 2009
Posts: 88
Well Bear ,

Basically i want to bring back the control back the text input when user enters a wrong input and tabs out .
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

Then I was correct -- you want to set focus to the input element. In that case, by examples stand.
Naveen puttu
Ranch Hand

Joined: Sep 15, 2009
Posts: 88
Well Bear ,

I want the typing cursor to sit the text box and it doesnt seem to happen for some reason with your example

may be i wasnt clear about what i wanted


Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
The cursor should be in the textbox when you use focus(). If it is not, I would guess that the selector you are using is wrong.

Try to do



and see if the background color changes

Eric
Naveen puttu
Ranch Hand

Joined: Sep 15, 2009
Posts: 88
Eric ,

When i use



I get the value of bid , but when i do



I get an error message in firebug which says

What am i doing wrong
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6

is not correct

using [0] or get(0) returns a DOM Object, not a jQuery Object. DOM objects do not have .css()

Why would you use [id=] when you can do



Eric

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: focusing on an input element with jquery