• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

focusing on an input element with jquery

 
Naveen puttu
Ranch Hand
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64181
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64181
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64181
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then I was correct -- you want to set focus to the input element. In that case, by examples stand.
 
Naveen puttu
Ranch Hand
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic