• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

focusing on an input element with jquery

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic